#arc093c. [arc093_c]Bichrome Spanning Tree
[arc093_c]Bichrome Spanning Tree
Problem Statement
We have an undirected weighted graph with vertices and edges. The -th edge in the graph connects Vertex and Vertex , and has a weight of . Additionally, you are given an integer .
Find the number of ways to paint each edge in this graph either white or black such that the following condition is met, modulo :
- The graph has a spanning tree that contains both an edge painted white and an edge painted black. Furthermore, among such spanning trees, the one with the smallest weight has a weight of .
Here, the weight of a spanning tree is the sum of the weights of the edges contained in the spanning tree.
Constraints
- ()
- ()
- If , then and .
- ()
- The given graph is connected.
- All input values are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3 3
2
1 2 1
2 3 1
3 1 1
Sample Output 1
6
Sample Input 2
3 3
3
1 2 1
2 3 1
3 1 2
Sample Output 2
2
Sample Input 3
5 4
1
1 2 3
1 3 3
2 4 6
2 5 8
Sample Output 3
0
Sample Input 4
8 10
49
4 6 10
8 4 11
5 8 9
1 8 10
3 8 128773450
7 8 10
4 2 4
3 4 1
3 1 13
5 2 2
Sample Output 4
4