Intersecting Paths
Time limit2sMemory limit1024 MB
For a layered directed graph, compute the signed count of vertex-disjoint path sets by crossing parity modulo 998244353.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Combinatorics, Matrix
- Solved
- No attempts yet
Problem
There is a directed graph whose vertices can be partitioned into levels. Level has vertices, and levels and have the same number of vertices, that is, . For each level , we have . Every edge whose tail is a vertex in level has its head in level . No edge enters level , and no edge leaves level .
We want to choose a set of paths. Each path starts at a vertex in level and ends at a vertex in level , and a vertex may occur in at most one path. More formally, number the vertices in each level as . Each path can be written as a -tuple , meaning that the path passes through vertex in level , and for every there is an edge from vertex in level to vertex in level .
When the paths are drawn on paper, they produce intersections. For paths , suppose the edges between level and level on the paths are and . If , the two paths produce an intersection in level . The number of intersections of two paths is the sum of their intersections over levels through . The number of intersections of a set of paths is the sum of the numbers of intersections over all pairs of different paths. In the example below, three paths produce three intersections in total. The red dots are the intersections.
Now we want to compute the number of sets of paths with an even number of intersections minus the number of sets of paths with an odd number of intersections. Two sets of paths are the same if and only if the collections of -tuples corresponding to their paths are the same. Since a set of paths is a set, the order of the paths does not matter. The result can be large, so output it modulo the prime .
Input
There are multiple test cases in one input file. The first line contains an integer , the number of test cases.
For each test case, the first line contains an integer , the number of levels.
The second line contains integers , the numbers of vertices in each level. It is guaranteed that and that for all with .
The third line contains integers , the numbers of edges between vertices in level and vertices in level . It is guaranteed that .
There are input blocks following. The -th block contains lines. Each line contains two integers , meaning that level vertex has an edge to level vertex .
It is guaranteed that there are no parallel edges.
Output
The output has lines. Each line contains the number of sets of paths with an even number of intersections minus the number of sets of paths with an odd number of intersections, modulo .
Constraints
For all test sets, , , and . In each test set, at most one test case has .
Hint
There are two sets of paths with an even number of intersections and one set with an odd number of intersections, so the output is 1.
In set 1, the two paths are and in the -tuple notation. There is one intersection in total. Swapping the two paths gives the same set of paths, and the same set is not counted twice.
In set 2, the two paths are and . There are 2 intersections.
In set 3, the two paths are and . There are 0 intersections.