Intersecting Paths

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

There is a directed graph such that the vertices may be partitioned into kk levels. The ii-th level has n_in\_i vertices, and the number of vertices in level 11 and level kk are the same (i.e. n_1=n_kn\_1 = n\_k), and for the jj-th level (2jk1)(2 \le j \le k-1), we have n_1n_j2n_1n\_1 \le n\_j \le 2n\_1. Edges whose tails are vertices in level jj (1j<k)(1 \le j < k) will only go to vertices in level j+1j+1. There are no edges whose heads are vertices in level 11, and there are no edges whose tails are vertices in level kk.

Now we want to choose a set of paths with n_1n\_1 paths in the set. Each path in the set starts from a vertex in level 11 and ends in a vertex in level kk, and a vertex in the graph may only occur in one path. More formally, if we number the vertices in each level by 1,2,,n_i1, 2, \dots, n\_i, then each path may be written as a kk-tuple (p_1,p_2,,p_k)(p\_1, p\_2, \dots, p\_k) denoting that the path passes through vertex p_jp\_j (1p_jn_j)(1 \le p\_j \le n\_j) in level jj, and there exists an edge connecting vertex p_jp\_j in level jj (1j<k)(1 \le j < k) to vertex p_j+1p\_{j+1} in level j+1j+1.

If we draw the paths on paper, we know they will produce some intersections. For paths P,QP,Q, suppose the edges between level jj and level j+1j+1 in the paths are (P_j,P_j+1)(P\_j,P\_{j+1}) and (Q_j,Q_j+1)(Q\_j,Q\_{j+1}), then if (P_jQ_j)×(P_j+1Q_j+1)<0(P\_j - Q\_j) \times (P\_{j+1} - Q\_{j+1}) < 0, we say the two paths produce an intersection in level jj. The number of intersections of two paths is the sum of number of intersections they produce in level 1,2,,j11, 2, \dots, j-1. For a set of paths, the number of intersections is defined to be sum of number of intersections between two different paths. In the following example, there are 3 paths producing 3 intersections in total. The red dots are the intersections:

Now we want to compute the difference between the number of sets of paths with even number of intersections and the number of sets of paths with odd number of intersections. Two sets of paths are considered to be the same if and only if the kk-tuple corresponding to the n_1n\_1 paths are the same. Since the final result might be large, please output the answer modulo 998,244,353998\\,244\\,353, a prime.

입력

There are multiple test cases in one test set. The first line is an integer TT denoting the number of test cases.

For each test case, the first line is an integer kk denoting there are kk levels of vertices.

The second line contains kk integers n_1,n_2,,n_kn\_1, n\_2, \dots, n\_k denoting the number of vertices in each level. It is guaranteed that n_1=n_kn\_1 = n\_k and n_1n_i2n_1n\_1 \le n\_i \le 2n\_1 for 2ik12 \le i \le k-1.

The third line contains k1k-1 integers m_1,m_2,,m_k1m\_1, m\_2, \dots, m\_{k-1} denoting the number of edges between vertices in level jj and vertices in level j+1j+1. It is guaranteed that m_jn_j×n_j+1m\_j \le n\_j \times n\_{j+1}.

There are k1k-1 sections of input following. The jj-th section (1j<k)(1 \le j < k) contains m_jm\_j lines. Each line contains two integers u,vu,v denoting vertex uu in level jj has an edge whose head is vertex vv in level j+1j+1.

It is guaranteed there are no parallel edges.

출력

The output has TT lines. Each line contains an integer denoting the answer (the number of sets of paths with even number of intersections minus the number of sets of paths with odd number of intersections) modulo 998,244,353998\\,244\\,353.

제한

For all test sets, 2k1002 \le k \le 1002n_i1002 \le n\_i \le 1001T51 \le T \le 5. It is guaranteed in each test set there will be at most one test case with n_1>10n\_1 > 10 in each test set.

힌트

There are two sets of paths with even number of intersections and one with 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. Notice if we swap the two paths, we obtain the same set of paths, and we do not count the same set 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.