After escaping the flying eggs and finding used candy, you decide to do some trick-or-treating yourself! Your neighborhood has houses with roads connecting them. You live in house and want to travel along these roads to reach your friend's house . There are types of candy and roads, with each road serving candy of type . For every road that you traverse, you will receive one candy of type . Since you don't want cavities, you want to reach your friend's house while collecting at most one of each type of candy. How many distinct paths can you take from house to house while satisfying this constraint?
A few important things to note:
- Since the answer can be quite large, please output the answer modulo .
- Your paths do not have to be simple (i.e. they may contain cycles).
- Once you reach house , you may not traverse any more edges.
- Two paths are different if for some , the th edge on one path is different from the th edge on the other.
- There may be multiple roads connecting the same pair of houses but no edge will connect the same house (e.g. holds true).
- The graph is not guaranteed to be connected.
Constraints
In all test cases, , , .
Note: You must solve all preceding subtasks in order to earn points for a specific subtask.
Subtask 1 [10%]
; in addition, the graph is guaranteed to form a tree (i.e. , no cycles exist, and the graph is connected).
Subtask 2 [20%]
Subtask 3 [70%]
No further constraints.
Input Specification
First line: , , and : the number of houses, number of roads, and number of candy types respectively.
Next lines: , , and (, , ), indicating a bidirectional road that connects houses and and serves candy of type .
Output Specification
One integer: the number of distinct paths you can take while satisfying the described constraint modulo .
Sample Input 1
6 7 5
2 1 1
2 3 2
2 4 2
3 5 2
2 5 3
5 6 4
4 5 5
Output for Sample Case 1
2
Explanation for Sample Case 1
See a diagram of the neighbourhood below:
The two ways to reach node without receiving duplicate candies are
and
.
Sample Input 2
4 4 1
1 2 1
1 3 1
2 4 1
3 4 1
Output for Sample Case 2
0
Explanation for Sample Case 2
There is no possible sequence of roads you can traverse without receiving duplicate candy.
Comments