King Animesh decides to have a voyage to the sun
Time limit1sMemory limit1024 MB
Given some edge costs of a complete bipartite graph where every perfect matching has equal cost, find the sum of squares of all N^2 edge costs modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Union-find, Math, Graph
- Solved
- No attempts yet
Statement
King Animesh has decided to go on a voyage to the Sun and will take only one of his courtiers with him. His two favorite courtiers, Arjun and Praveen, are competing for the trip. Arjun is a great archer and plans to impress the king by showing his skill. Praveen is smart and knows exactly what the King likes: Graphs of Perfection.
A Graph of Perfection is a weighted complete bipartite graph with vertices in which every perfect matching has the same cost. The cost of a perfect matching is the sum of the costs of its edges.
Praveen found one such graph in the Forest of Perfection and kept it in a box. The vertices in the left column are labeled , and the vertices in the right column are labeled . The cost of the edge between left vertex and right vertex is .
Arjun made a hole in the box, and some edges fell out. Praveen claims he can assign costs to the missing edges so that the graph is a Graph of Perfection again. The remaining edges determine the assignment uniquely. He does not tell the King this. The King asks him to report the sum of the squares of all edge costs modulo . You are given the costs of the edges still in the box. Find the required sum.
Input
The first line contains two integers and . Each of the next lines contains three integers , , and . This means the edge from left vertex to right vertex is still present, and its cost is .
It is guaranteed that exactly one way to assign costs to the missing edges makes the given graph a Graph of Perfection.
Output
For each test case, output a single integer on one line: the answer.
Constraints
- . This constraint does not apply to the missing edges, whose costs need not satisfy it.
Hint
A complete bipartite graph has an edge between every node in the left column and every node in the right column.
A perfect matching in this graph is a set of edges such that no two edges share an endpoint.