luogu#P7736. [NOI2021] 路径交点
[NOI2021] 路径交点
Problem Description
Xiao L has a directed graph. The vertices in the graph can be divided into layers. Layer has vertices. The numbers of vertices in layer and layer are the same, i.e., . For layer (), it holds that . For vertices in layer (), all edges starting from them only go to vertices in layer . There are no edges pointing to vertices in layer , and vertices in layer do not have outgoing edges to any other vertices.
Now Xiao L wants to choose paths from this graph. Each path starts at a vertex in layer and ends at a vertex in layer . Also, it is required that each vertex in the graph appears in at most one path. More specifically, number the vertices in each layer as . Then each path can be written as a -tuple , meaning that this path passes through vertex () in layer in order, and the vertex in layer () has an edge to vertex in layer .
Xiao L drew these paths on paper and found that they produce some intersection points. For two paths , let their edges between layer and layer be and , respectively. If
then they are said to produce an intersection point after layer . The number of intersections between two paths is the total number of intersection points they produce after layers . For the whole path selection (path scheme), its number of intersections is the sum of the numbers of intersections over all unordered pairs of distinct paths. For example, the figure below shows an example with paths and intersections, where the red points are intersections.

Xiao L now wants to know how many more path schemes with an even number of intersections there are than path schemes with an odd number of intersections. Two path schemes are considered the same if and only if their -tuples, written in the order of the starting vertex indices in the first layer, are identical. Since the final result may be very large, please output it modulo (a large prime).
Input Format
This problem contains multiple test cases. The first line contains a positive integer , indicating the number of test cases. For each test case:
The first line contains a positive integer , indicating that there are layers of vertices.
The second line contains integers , indicating the number of vertices in each layer. It is guaranteed that , and ().
The third line contains integers , indicating the number of edges from layer to layer . It is guaranteed that .
Then there are blocks of input. The -th block () contains lines. Each line has two integers , meaning that vertex in layer has a directed edge to vertex in layer .
The input guarantees that there are no multiple edges in the graph.
Output Format
Output lines. Each line contains one integer, the answer for 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
Hint
[Sample Explanation #1]
There are schemes with an even number of intersections and scheme with an odd number of intersections, so the answer is .
If you swap path and path in the table below, you will get the same scheme. For example, the scheme where path is and path is is the same as scheme , so it will not be counted separately.
| Path scheme | Path | Path | Total intersections |
|---|---|---|---|
[Sample #2]
See the attachments xpath2.in and xpath2.ans.
The constraints of this sample are consistent with test points .
[Sample #3]
See the attachments xpath3.in and xpath3.ans.
The constraints of this sample are consistent with test points .
[Sample #4]
See the attachments xpath4.in and xpath4.ans.
The constraints of this sample are consistent with test points .
[Constraints]
For all testdata: , , .
In each test point, it is guaranteed that there is only test case with .
| Test point ID | Special properties | ||
|---|---|---|---|
| None | |||
| A, B | |||
| A | |||
| None | |||
| A, B | |||
| A | |||
| None |
Special property A: For all (), .
Special property B: It is guaranteed that the total number of path schemes is at most .
Translated by ChatGPT 5