eolymp
bolt
Try our new interface for solving problems
Problems

Sum of Distances

Sum of Distances

Bessie has a collection of connected, undirected graphs G1, G2, ..., Gk. Gi (1ik) has exactly ni (ni2) vertices labeled 1..ni and mi (mini1) edges. Each Gi may contain self-loops, but not multiple edges between the same pair of vertices.

Now Elsie creates a new undirected graph G with n1 * n2 * ... * nk vertices, each labeled by a k-tuple (j1, j2,..., jk) where 1jini. In G, two vertices (j1, j2,...,jk) and (k1, k2,..., kk) are connected by an edge if for all i (1ik), ji and ki are connected by an edge in Gi.

Define the distance between two vertices in G that lie in the same connected component to be the minimum number of edges along a path from one vertex to the other. Compute the sum of the distances between vertex (1, 1,..., 1) and every vertex in the same component as it in G, modulo 109 + 7.

Input

The first line contains k (2k5 * 104), the number of graphs.

Each graph description starts with ni and mi on a single line, followed by mi edges.

Consecutive graphs are separated by newlines for readability. It is guaranteed that ∑ ni105 and ∑ mi2 * 105.

Output

Print the sum of the distances between vertex (1, 1,..., 1) and every vertex that is reachable from it, modulo 109 + 7.

Example 1

G contains 2 * 4 = 8 vertices, 4 of which are not connected to vertex (1, 1). There are 2 vertices that are distance 1 away from (1, 1) and 1 that is distance 2 away. So the answer is 2 * 1 + 1 * 2 = 4.

Example 2

G contains 4 * 6 * 7 = 168 vertices, all of which are connected to vertex (1, 1, 1). The number of vertices that are distance i away from (1, 1, 1) for each i ∈ [1, 7] is given by the i-th element of the following array: [4, 23, 28, 36, 40, 24, 12].

Time limit 1 second
Memory limit 128 MiB
Input example #1
2

2 1
1 2

4 4
1 2
2 3
3 4
4 1
Output example #1
4
Input example #2
3

4 4
1 2
2 3
3 1
3 4

6 5
1 2
2 3
3 4
4 5
5 6

7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
Output example #2
706
Source 2021 USACO January, Platinum