Problem: You are given a tree (a connected undirected graph without cycles) of vertices. Each of the edges of the tree is colored in either black or red.
You are also given an integer . Consider sequences of vertices. Let's call a sequence good if it satisfies the following criterion:
- We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from and ending at .
- Start at , then go to using the shortest path between and , then go to in a similar way, and so on, until you travel the shortest path between and .
- If you walked over at least one black edge during this process, then the sequence is good.
Consider the tree on the picture. If then the following sequences are good: , and . The following sequences are not good: , , .
There are sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo .
Input Format: The first line contains two integers and (, ), the size of the tree and the length of the vertex sequence.
Each of the next lines contains three integers , and (, ), where and denote the endpoints of the corresponding edge and is the color of this edge ( denotes red edge and denotes black edge).
Output Format: Print the number of good sequences modulo .
Note: In the first example, all sequences () of length except the following are good:
In the second example, all edges are red, hence there aren't any good sequences.