#CF2239F. 彩色作品 / F. Colorful Works
彩色作品 / F. Colorful Works
F. Colorful Works
Source: Codeforces 2239F
Contest: Codeforces Round 1105 (Div. 1)
Time limit: 5 seconds
Memory limit: 256 megabytes
Statement
Gold14526 is a painter. He can paint with colors, numbered . Color has a constraint interval .
A work is defined as a rooted tree where every edge is colored (with one of the colors). A work is called colorful if the following conditions are satisfied:
- For any three nodes , if edges and both exist, they must have different colors.
- For all colors , let denote the number of edges of color on the simple path from node to the root. Then .
Two works and are defined as isomorphic if and only if the following two conditions are met:
- ;
- There exists a bijection such that:
- Let be the root of and be the root of . Then ;
- For any , we have that , and the color of edge is the same as the color of edge .
Gold14526 wants to know the maximum number of colorful works he can choose such that the works are pairwise non-isomorphic. Output the answer modulo .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains an integer () — denoting the number of colors.
The following lines each contain two integers, the -th of them and (, ) — denoting the constraint interval of the -th color.
It is guaranteed that the sum of over all test cases does not exceed .
Let . Then it is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output or , representing the maximum number of works that can be chosen modulo .
Note
In the first test case, the constraints for both colors are . This means on any simple path from the root, there can be at most edge of color and at most edge of color . There are exactly valid pairwise non-isomorphic trees:
- tree with node: just the root.
- trees with nodes: the root is connected to a child by an edge of color , or by an edge of color .
- trees with nodes:
- the root is connected to two children by edges of color and respectively.
- a path of edges from the root, colored then .
- a path of edges from the root, colored then .
- trees with nodes:
- the root has a child via color (which further has a child via color ), and another child via color .
- the root has a child via color (which further has a child via color ), and another child via color .
- tree with nodes: the root is connected to two children by colors and , and each of these children has exactly one child of the opposite color.
Since , the output is . In the second test case, the constraints for both colors are . Every valid tree must satisfy the maximum count of each color on the paths to be exactly . Therefore, the tree must contain at least one edge of color and at least one edge of color . There are exactly valid trees:
- trees with nodes: the root connected to two children by colors and ; a path colored then ; a path colored then .
- trees with nodes: same as the two -node trees described in the first test case.
- tree with nodes: same as the -node tree described in the first test case.
Since , the output is .
Examples
4
2
0 1
0 1
2
1 1
1 1
3
0 2
0 1
0 1
3
1 2
1 1
1 1
1
0
1
1