409386: GYM103495 L Tree Game

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

L. Tree Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

With great enthusiasm, Alice finds a new game and she is going to play it with Bob.

There's a tree with $$$n$$$ nodes rooted at node $$$1$$$. Each node $$$u$$$ has an associated value $$$a_u$$$, where $$$a_1, a_2, \ldots, a_n$$$ is a permutation of $$$n$$$ (every integer from $$$1$$$ to $$$n$$$ occurs exactly once). Let's define the rearrangement operation on a node $$$u$$$ as follow:

  • Remove all the values on node $$$u$$$ and all the children directly connected to $$$u$$$. Then reassign the values to them so that $$$a_1, a_2, \ldots, a_n$$$ is still a permutation of $$$n$$$.

After the operation, we call the node $$$u$$$ rearranged. Alice can rearrange a node if and only if it has no directly connected children that are not rearranged. What's more, she can rearrange a node at most once. If Alice can make $$$a_u=u$$$ for every node $$$u$$$, she will win this game.

Alice wants to challenge the game and asks Bob to draw a tree and assign the initial values for every node. Now Bob has already drawn a tree and assigned some values, but before he finishes assigning all the values, he finds that there are some ways of assignment that can make Alice never win the game, which will let Alice down. To avoid this, Bob wants to know how many possible ways of assignment there are that Alice has a chance to win the game. Since the answer can be too large, please output the answer modulo $$$998244353$$$.

Input

The first line of the input contains an integer $$$T$$$ ($$$1 \leq T \leq 10000$$$), representing the number of test cases.

The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 200000$$$), representing the number of nodes.

The second line of each test case contains $$$n - 1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1 \leq p_i < i$$$), where $$$p_i$$$ represents the parent of node $$$i$$$.

The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq n$$$), representing the initial value of each node. $$$a_i=0$$$ means that node $$$i$$$ has not been assigned a value yet. It is guaranteed that all non-zero $$$a_i$$$ are distinct.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200000$$$.

Output

For each test case, output an integer in a single line, indicating the number of possible ways of assignment. Since the answer can be too large, please output the answer modulo $$$998244353$$$.

ExampleInput
2
7
1 2 2 3 2 4
0 0 0 2 0 0 0
7
1 2 3 3 3 1
0 0 0 0 0 0 0
Output
12
288

加入题单

算法标签: