Under construction

Still being built. Things may change

Intersecting Paths

Time limit2sMemory limit1024 MB

Summary
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 kk levels. Level ii has nin_i vertices, and levels 11 and kk have the same number of vertices, that is, n1=nkn_1=n_k. For each level jj (2≤j≤k−1)(2 \le j \le k-1), we have n1≤nj≤2n1n_1 \le n_j \le 2n_1. Every edge whose tail is a vertex in level jj (1≤j<k)(1 \le j < k) has its head in level j+1j+1. No edge enters level 11, and no edge leaves level kk.

We want to choose a set of n1n_1 paths. Each path starts at a vertex in level 11 and ends at a vertex in level kk, and a vertex may occur in at most one path. More formally, number the vertices in each level as 1,2,…,ni1,2,\dots,n_i. Each path can be written as a kk-tuple (p1,p2,…,pk)(p_1,p_2,\dots,p_k), meaning that the path passes through vertex pjp_j (1≤pj≤nj)(1 \le p_j \le n_j) in level jj, and for every jj (1≤j<k)(1 \le j < k) there is an edge from vertex pjp_j in level jj to vertex pj+1p_{j+1} in level j+1j+1.

When the paths are drawn on paper, they produce intersections. For paths P,QP,Q, suppose the edges between level jj and level j+1j+1 on the paths are (Pj,Pj+1)(P_j,P_{j+1}) and (Qj,Qj+1)(Q_j,Q_{j+1}). If (Pj−Qj)×(Pj+1−Qj+1)<0(P_j-Q_j)\times(P_{j+1}-Q_{j+1})<0, the two paths produce an intersection in level jj. The number of intersections of two paths is the sum of their intersections over levels 11 through k−1k-1. 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 kk-tuples corresponding to their n1n_1 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 998 244 353998\,244\,353.

Input

There are multiple test cases in one input file. The first line contains an integer TT, the number of test cases.

For each test case, the first line contains an integer kk, the number of levels.

The second line contains kk integers n1,n2,…,nkn_1,n_2,\dots,n_k, the numbers of vertices in each level. It is guaranteed that n1=nkn_1=n_k and that n1≤ni≤2n1n_1 \le n_i \le 2n_1 for all ii with 2≤i≤k−12 \le i \le k-1.

The third line contains k−1k-1 integers m1,m2,…,mk−1m_1,m_2,\dots,m_{k-1}, the numbers of edges between vertices in level jj and vertices in level j+1j+1. It is guaranteed that mj≤nj×nj+1m_j \le n_j \times n_{j+1}.

There are k−1k-1 input blocks following. The jj-th block (1≤j<k)(1 \le j < k) contains mjm_j lines. Each line contains two integers u,vu,v, meaning that level jj vertex uu has an edge to level j+1j+1 vertex vv.

It is guaranteed that there are no parallel edges.

Output

The output has TT 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 998 244 353998\,244\,353.

Constraints

For all test sets, 2≤k≤1002 \le k \le 100, 2≤ni≤1002 \le n_i \le 100, and 1≤T≤51 \le T \le 5. In each test set, at most one test case has n1>10n_1>10.

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 (1,1,2)(1,1,2) and (2,3,1)(2,3,1) in the kk-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 (1,2,1)(1,2,1) and (2,1,2)(2,1,2). There are 2 intersections.

In set 3, the two paths are (1,2,1)(1,2,1) and (2,3,2)(2,3,2). There are 0 intersections.

Examples1

  1. Example 1

    Input
    1
    3
    2 3 2
    4 4
    1 1
    1 2
    2 1
    2 3
    1 2
    2 1
    3 1
    3 2
    
    Expected output
    1