#NOI2021B. 路径交点 (Path Intersections)

路径交点 (Path Intersections)

Description

Link has a directed graph. Vertices in the graph can be divided into kk layers, with nin_i vertices in the iith layer. The 11st layer and the kkth layer have the same number of vertices, or n1=nkn_1=n_k, and for the jjth (2jk1)(2 \leq j \leq k-1) layer, n1ni2n1(2ik1)n_1 \leq n_i \leq 2n_1(2 \leq i \leq k-1). For vertices in the jjth (1j<k)(1 \leq j < k) layer, edges pointing from them only point to vertices in the j+1j+1th layer. No edges point to vertices in the 11st layer, nor do vertices in the kkth layer have any edges pointing from them.

Link would choose n1n_1 paths from the graph. Each path starts from a vertex in the 11st layer and ends in a vertex in the kkth 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 1,2,...,ni1,2,...,n_i, then each path could be written as a kk-tuple (p1,p2,...,pk)(p_1,p_2,...,p_k), denoting that this path passes through vertex number pj(1pjnj)p_j(1 \leq p_j \leq n_j) in the jjth layer successively, and there exists a directed edge from vertex pjp_j in layer j(1j<k)j(1 \leq j < k) to vertex pj+1p_{j+1} in layer j+1j+1.

Link draws these paths on a paper and found them forming some intersections. For two paths P,QP,Q, let their edges between layer jj and j+1j+1 be (Pj,Pj+1)(P_j,P_{j+1}) and (Qj,Qj+1)(Q_j,Q_{j+1}), if:

(PjQj)×(Pj+1Qj+1)<0(P_j-Q_j) \times (P_{j+1}-Q_{j+1}) < 0

then we say that they form an intersection after the jjth layer. The total number of intersections between two paths is the sum of number of intersections formed between them after layers 1,2,...,k11,2,...,k-1. 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 33 paths and a total of 33 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 kk-tuples of their nin_i 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 998244353998244353 (a large prime).

Input Format

Each test contains multiple test cases. The first line contains a integer TT indicating the number of test cases. For each test case:

The first line contains an integers kk, indicating there are kk layers of vertices in total.

The second line contains kk integers n1,n2,,nkn_1,n_2,\dots,n_k, representing in order the number of vertices in each layer. It is guaranteed that n1=nkn_1=n_k, and n1ni2n1(2ik1)n_1 \leq n_i \leq 2n_1(2 \leq i \leq k-1).

The third line contains k1k-1 integers m1,m2,,mk1m_1,m_2,\dots,m_{k-1}, representing in order the number of directed edges from layer j(1j<k)j(1 \leq j < k) to layer j+1j+1. It is guaranteed that mjnj×nj+1m_j \leq n_j \times n_{j+1}.

This is followed by k1k-1 segments of input. The jjth (1j<k)(1 \leq j < k) segment contains mjm_j lines, each line consists of two integers u,vu,v, indicating there's a directed edge from vertex uu in layer jj to vertex vv in layer j+1j+1.

It is guaranteed that multiple edges don't exist in graphs.

Output Format

Output consists of TT lines, one integer each, indicating the answer to that test case modulo 998244353998244353.

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 22 arrangements with an even number of intersections and 11 arrangement with an odd number of intersections, so the answer is 11.

Exchanging path 11 and path 22 in the table below results in the same path arrangement. For example, an arrangement with (2,3,1)(2,3,1) as path 11 and $(1,1,2) as path 22 is the same as arrangment 11, so won't be included in the answer.

Path Arrangement Path 11 Path 22 Total of Intersections
11 (1,1,2)(1,1,2) (2,3,1)(2,3,1) 11
22 (1,2,1)(1,2,1) (2,1,2)(2,1,2) 22
33 (2,3,2)(2,3,2) 00

Sample 2

See files xpath2.in and xpath2.ans under participant directory.
Constraints of this sample is the same as test cases 787 \sim 8.

Sample 3

See files xpath3.in and xpath2.ans under participant directory.
Constraints of this sample is the same as test cases 9109 \sim 10.

Sample 4

See files xpath4.in and xpath4.ans under participant directory.
Constraints of this sample is the same as test cases 141514 \sim 15.

Constraints and Specification

For all the tests: 2k1002 \leq k \leq 100, 2n11002 \leq n_1 \leq 100, 1T51 \leq T \leq 5.

In each test, it is guaranteed that only one test case has n1>10n_1 > 10.

Test Number k=k= n1n_1 \leq Special Case
141 \sim 4 22 1010 None
565 \sim 6 1010 A,B\text{A,B}
787 \sim 8 A\text{A}
9109 \sim 10 None
111311 \sim 13 22 100100
141514 \sim 15 100100 A,B\text{A,B}
161716 \sim 17 A\text{A}
182018 \sim 20 None

Special case A\text{A}: for all i(2ik1)i(2 \leq i \leq k-1), ni=n1n_i=n_1.
Special case B\text{B}: it is assured that there are at most 11 valid path arrangement.