Problem: Pak Chanek has a directed acyclic graph (a directed graph that does not have any cycles) containing vertices. Vertex has edges directed away from that vertex. The -th edge of vertex that is directed away from it, is directed towards vertex and has an integer (). Another information about the graph is that the graph is shaped in such a way such that each vertex can be reached from vertex via zero or more directed edges.
Pak Chanek has an array that is initially empty.
Pak Chanek defines the function dfs as follows:
Note that the function does not keep track of which vertices have been visited, so each vertex can be processed more than once.
Let's say Pak Chanek does dfs(1) once. After that, Pak Chanek will get an array containing some elements or . Define an inversion in array as a pair of indices () such that . How many different inversions in are there if Pak Chanek does dfs(1) once? Since the answer can be very big, output the answer modulo .
Input Format: The first line contains a single integer () — the number of vertices in the graph. The following lines contain the description of each vertex from vertex to vertex .
The first line of each vertex contains a single integer () — the number of edges directed away from vertex .
The -th of the next lines of each vertex contains two integers and (; ) — an edge directed away from vertex that is directed towards vertex and has an integer . For each , the values of , , ..., are pairwise distinct.
It is guaranteed that the sum of over all vertices does not exceed . There are no cycles in the graph. Each vertex can be reached from vertex via zero or more directed edges.
Output Format: An integer representing the number of inversions in if Pak Chanek does dfs(1) once. Since the answer can be very big, output the answer modulo .
Note: The following is the dfs(1) process on the graph.
In the end, . All of its inversions are , , , and .