There is a binary tree with the root
at vertex 1, and each
vertex u in the tree has a
weight a_u. You can perform the
following two operations:
1.
If vertex u has left child v and right child ww, and its left child vvhas a right child xx, then you can perform a "rotation" on these four vertices.
Let a'_u=a_v, a'_w=a_u, a'_x=a_w, a'_v=a_x and then let a_u=a'_u, a_v=a'_v, a_w=a'_w, a_x=a'_x.
2.
If vertex u has left child v and right child w, and its right child w has a left child x, then you can perform a
"rotation" on these four vertices. Let a'_u=a_v, a'_w=a_u, a'_x=a_w, a'_v=a_x and then let a_u=a'_u,
a_v=a'_v, a_w=a'_w, a_x=a'_x.
Find the number of different point weight arrays a that can be obtained by
performing a series of operations, modulo 998244353.
The input consists of
multiple test cases. The first line contains a single integer T(1≤T≤1000) - the number of test cases.
Desc
The first line of each test case contains
one integer n(1≤n≤10^5) - the number of vertices in the tree.
The second line contains nn integers a_1,
a_2,……, a_n (1≤ai≤n) - the point weights.
Each of the following nn lines contains two
integers l_i,
r_i(0≤li,ri≤n) - the left child and right child of
vertex ii. If a child does not exist,
it is represented by 0.
It's guaranteed that ∑n≤10^6.
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
2
5040