#NOI2021B. 路径交点 (Path Intersections)
路径交点 (Path Intersections)
Description
Link has a directed graph. Vertices in the graph can be divided into layers, with vertices in the th layer. The st layer and the th layer have the same number of vertices, or , and for the th layer, . For vertices in the th layer, edges pointing from them only point to vertices in the th layer. No edges point to vertices in the st layer, nor do vertices in the th layer have any edges pointing from them.
Link would choose paths from the graph. Each path starts from a vertex in the st layer and ends in a vertex in the th layer, and it is required that each vertex in the graph would only appear in at most one of the paths. More specifically, if vertices in each layer are numbered as , then each path could be written as a -tuple , denoting that this path passes through vertex number in the th layer successively, and there exists a directed edge from vertex in layer to vertex in layer .
Link draws these paths on a paper and found them forming some intersections. For two paths , let their edges between layer and be and , if:
then we say that they form an intersection after the th layer. The total number of intersections between two paths is the sum of number of intersections formed between them after layers . For the whole path arrangement, its intersection count equals the sum of number of intersections between all distinct pairs of paths. For example, the figure below is a case with paths and a total of intersections, with red points indicating the intersections.
Link now wonders how many more arrangements with an even number of intersections are there than those with an odd number of intersections. Two path arrangements are the same if and only if the -tuples of their paths ordered by their starting points' number in the first layer are all the same. Since the result may be large, please output it modulo (a large prime).
Input Format
Each test contains multiple test cases. The first line contains a integer indicating the number of test cases. For each test case:
The first line contains an integers , indicating there are layers of vertices in total.
The second line contains integers , representing in order the number of vertices in each layer. It is guaranteed that , and .
The third line contains integers , representing in order the number of directed edges from layer to layer . It is guaranteed that .
This is followed by segments of input. The th segment contains lines, each line consists of two integers , indicating there's a directed edge from vertex in layer to vertex in layer .
It is guaranteed that multiple edges don't exist in graphs.
Output Format
Output consists of lines, one integer each, indicating the answer to that test case modulo .
1
3
2 3 2
4 4
1 1
1 2
2 1
2 3
1 2
2 1
3 1
3 2
1
Sample Explanation 1
There are arrangements with an even number of intersections and arrangement with an odd number of intersections, so the answer is .
Exchanging path and path in the table below results in the same path arrangement. For example, an arrangement with as path and $(1,1,2) as path is the same as arrangment , so won't be included in the answer.
Path Arrangement | Path | Path | Total of Intersections |
---|---|---|---|
Sample 2
See files xpath2.in and xpath2.ans under participant directory.
Constraints of this sample is the same as test cases .
Sample 3
See files xpath3.in and xpath2.ans under participant directory.
Constraints of this sample is the same as test cases .
Sample 4
See files xpath4.in and xpath4.ans under participant directory.
Constraints of this sample is the same as test cases .
Constraints and Specification
For all the tests: , , .
In each test, it is guaranteed that only one test case has .
Test Number | Special Case | ||
---|---|---|---|
None | |||
None | |||
None |
Special case : for all , .
Special case : it is assured that there are at most valid path arrangement.