#P1857G. Counting Graphs
Counting Graphs
No submission language available for this problem.
Description
Given a tree consisting of vertices. A tree is a connected undirected graph without cycles. Each edge of the tree has its weight, .
Your task is to count the number of different graphs that satisfy all four conditions:
- The graph does not have self-loops and multiple edges.
- The weights on the edges of the graph are integers and do not exceed .
- The graph has exactly one minimum spanning tree.
- The minimum spanning tree of the graph is the given tree.
Two graphs are considered different if their sets of edges are different, taking into account the weights of the edges.
The answer can be large, output it modulo .
The first line contains an integer () — the number of test cases.
The first line of each test case contains two integers and () — the number of vertices and the upper bound of the weights.
Then follow lines describing the tree, the -th line contains three integers , , and () — an edge in the tree with weight .
It is guaranteed that the sum of for all tests does not exceed .
For each test, output the number of different graphs that satisfy the conditions, modulo .
Input
The first line contains an integer () — the number of test cases.
The first line of each test case contains two integers and () — the number of vertices and the upper bound of the weights.
Then follow lines describing the tree, the -th line contains three integers , , and () — an edge in the tree with weight .
It is guaranteed that the sum of for all tests does not exceed .
Output
For each test, output the number of different graphs that satisfy the conditions, modulo .
Note
In the first sample, there is only one graph, which is the given tree.
In the second samle, the given tree looks like this:

