#P9866. C. Rotation

C. Rotation

C. Rotation

Problem Description

There is a binary tree with the root at vertex 11, and each vertex uu in the tree has a weight aua_u. You can perform the following two operations:

  1. If vertex uu has left child vv and right child ww, and its left child vv has a right child xx, then you can perform a "rotation" on these four vertices. Let au=av,aw=au,ax=aw,av=axa'_u=a_v, a'_w=a_u, a'_x=a_w, a'_v=a_x, and then let au=au,av=av,aw=aw,ax=axa_u=a'_u, a_v=a'_v, a_w=a'_w, a_x=a'_x.
  2. If vertex uu has left child vv and right child ww, and its right child ww has a left child xx, then you can perform a "rotation" on these four vertices. Let au=av,aw=au,ax=aw,av=axa'_u=a_v, a'_w=a_u, a'_x=a_w, a'_v=a_x, and then let au=au,av=av,aw=aw,ax=axa_u=a'_u, a_v=a'_v, a_w=a'_w, a_x=a'_x. Find the number of different point weight arrays aa that can be obtained by performing a series of operations, modulo 998244353998244353.

Input

The input consists of multiple test cases. The first line contains a single integer TT (1T10001 \le T \le 1000) - the number of test cases. Description of the test cases follows. The first line of each test case contains one integer nn (1n1051 \leq n \leq 10^5) - the number of vertices in the tree. The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \leq a_i \leq n) - the point weights. Each of the following nn lines contains two integers li,ril_i, r_i (0li,rin0 \le l_i, r_i \le n) - the left child and right child of vertex ii. If a child does not exist, it is represented by 00. It's guaranteed that n106\sum n \leq 10^6.

Output

For each test case, print a single integer - the number of different point weight arrays aa that can be obtained by performing a series of operations, modulo 998244353998244353.

Sample Input

2
4
1 2 2 1
2 3
0 4
0 0
0 0
8
1 2 3 4 5 6 7 8
2 3
4 5
6 7
0 0
0 0
0 8
0 0
0 0

Sample Output

2
5040

Source

2023“钉耙编程”中国大学生算法设计超级联赛(7)