Superstition

Time limit: 3000 ms
Memory limit: 256 MB

Today is the long-awaited day for all school students – the first holiday for the new school year. Our main heroine Deni is now in grade 10. She prepared for today – she found that there are NN stores downtown and she plans to visit some of them with her friends. But Deni and her friends don’t like some of the connections between the stores and they won’t use them. So they have made a list of MM pairs of stores such that a pair (x,y)(x, y) is from the list if they like the connection from store xx to yy and of course they can reach store xx from yy and for every pair they have determined the time for travelling the connection (it is the same in both directions). There are no pairs with stores with the same numbers and there are no duplicate pairs.

Deni is very superstitious and one of the superstitions in which she believes is that the total time for travelling must be divisible by DD. Deni and her friends don’t have unlimited time so the maximum time they can spend travelling is KK. As all girls, Deni is very curious and she starts to count the number of different routes for visiting some of the stores (a store can be visited more than once). Unfortunately, this number can be large so Deni remembers that she knows you – very good programmer and asks you to write the program superstition, which counts the number of valid routes. One route is valid if it uses the connections from the list of pairs, the total time for travelling is divisible by DD and it is K\le K. Two routes are different if there is a difference in the sequences of visited stores. You immediately notice that the answer can be very large and thus Deni tells you that she only wants the remainder when the answer is divided by 109+710^9+7.

Standard input

From the first line of the standard input read four integers NN, MM, DD and KK. From each of the next MM lines read three integers xix_i, yiy_i and tit_i – bidirectional connection between xix_i and yiy_i with time of travel tit_i (1iM1 \le i \le M).

Standard output

The number of different valid routes. Since this number may be quite large, you are required to print its remainder when divided by 109+710^9+7.

Constraints and notes

  • 2N802 \le N \le 80
  • 2M31602 \le M \le 3160
  • 2DK1092 \le D \le K \le 10^9
  • 1ti101 \le t_i \le 10

Subtasks and grading

SubtaskPointsNMDKFurther constrains

11

55

5\le 5

10\le 10

12\leq 12

12\le 12

There are no further constraints.

22

3030

80\le 80

3160\le 3\,160

104\le 10^4

104\le 10^4

There are no further constraints.

33

1010

20\le 20

190\le 190

109\le 10^9

109\le 10^9

D=KD=K and i=1Mti200\sum_{i=1}^{M}t_i \le 200

44

2020

20\le 20

190\le 190

109\le 10^9

109\le 10^9

i=1Mti200\sum_{i=1}^{M}t_i \le 200

55

1515

30\le 30

435\le 435

109\le 10^9

109\le 10^9

D=KD = K

66

2020

30\le 30

435\le 435

109\le 10^9

109\le 10^9

There are no further constraints.

Your program will get points for a given subtask only if all test cases for that subtask are passed successfully.

InputOutputExplanation
3 3 2 2
1 2 1
2 3 2
3 1 1
8

Here D=K=2D = K = 2 i.e. the needed routes are only with total time 22. They are:

  1. 1211-2-1
  2. 2122-1-2
  3. 3133-1-3
  4. 1311-3-1
  5. 232-3
  6. 323-2
  7. 2132-1-3
  8. 3123-1-2

Notice that stores and connections can be repeated more than once.

5 7 5 10
1 3 8
2 5 7
3 4 3
1 4 2
2 3 1
1 5 4
4 5 4
58

Because D<KD < K the needed routes are with total time 55 and 1010

5 9 2 20
1 2 1
2 3 2
3 1 1
3 4 1
4 5 2
5 3 1
1 5 1
2 4 1
2 5 1
989802661

Here the real answer is a big number so the output is only its remainder when divided by 109+710^9+7

5 7 5000000 5000000
1 3 8
2 5 7
3 4 3
1 4 2
2 3 1
1 5 4
4 5 4
598634781

Here the real answer is a big number so the output is only its remainder when divided by 109+710^9+7

Authenticate to use the workspace

Log In
Sign Up
Forgot Password?
or connect with