Under construction

Still being built. Things may change

King Animesh decides to have a voyage to the sun

Time limit1sMemory limit1024 MB

Summary
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 2N2N vertices in which every perfect matching has the same cost. The cost of a perfect matching is the sum of the costs of its NN edges.

Praveen found one such graph in the Forest of Perfection and kept it in a box. The NN vertices in the left column are labeled 1,2,…,N1, 2, \ldots, N, and the NN vertices in the right column are labeled N+1,N+2,…,2NN+1, N+2, \ldots, 2N. The cost of the edge between left vertex uu and right vertex vv is CuvC_{uv}.

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 N2N^2 edge costs modulo 109+710^9+7. You are given the costs of the edges still in the box. Find the required sum.

Input

The first line contains two integers NN and EE. Each of the next EE lines contains three integers uu, vv, and xx. This means the edge from left vertex uu to right vertex vv is still present, and its cost is Cuv=xC_{uv}=x.

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

  • 1≤N≤1051 \le N \le 10^5
  • 1≤E≤1061 \le E \le 10^6
  • 1≤u≤N1 \le u \le N
  • N+1≤v≤2NN+1 \le v \le 2N
  • 1≤x≤1051 \le x \le 10^5. 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 NN edges such that no two edges share an endpoint.

Examples1

  1. Example 1

    Input
    2 3
    1 3 2
    1 4 3
    2 3 5
    
    Expected output
    74