AT_agc079_a [AGC079A] Tree Contraction Sequence
Description
There is a rooted tree with $ N $ vertices. The vertices are numbered $ 1,2,\ldots,N $ , and the root is vertex $ 1 $ . The parent of each vertex $ v $ $ (2 \leq v \leq N) $ is vertex $ P_v $ , where $ 1 \leq P_v < v $ .
Additionally, each vertex holds an integer sequence. Initially, the sequence held by vertex $ v $ is $ (v) $ .
You will perform the following operation $ N-1 $ times.
- Choose a vertex $ v $ with $ 2 \leq v \leq N $ that has not been chosen in any previous operation, and let $ u $ be its parent in the current tree.
- Concatenate the sequence of vertex $ v $ to the end of the sequence of vertex $ u $ .
- Contract vertex $ v $ into its parent $ u $ . That is, delete vertex $ v $ and make all of its children into children of vertex $ u $ .
Find the number, modulo $ 998244353 $ , of operation sequences such that the sequence of the remaining vertex $ 1 $ after all operations equals $ (1,2,\ldots,N) $ . Here, two operation sequences are considered different if their sequences of chosen vertices are different.
You are given $ T $ test cases; solve each of them.
Input Format
The input is given from Standard Input in the following format:
> $ T $ $ \mathrm{case}_1 $ $ \mathrm{case}_2 $ $ \vdots $ $ \mathrm{case}_T $
Each test case is given in the following format:
> $ N $ $ P_2 $ $ P_3 $ $ \ldots $ $ P_N $
Output Format
For each test case, output the answer.
Explanation/Hint
### Sample Explanation 1
In the first test case, if the vertices are chosen in the order $ 3,2,4 $ , for example, the sequences change as follows.
- Choosing vertex $ 3 $ makes the sequence of vertex $ 2 $ equal to $ (2,3) $ .
- Choosing vertex $ 2 $ makes the sequence of vertex $ 1 $ equal to $ (1,2,3) $ .
- Choosing vertex $ 4 $ makes the sequence of vertex $ 1 $ equal to $ (1,2,3,4) $ .
There are $ 2 $ orders of choosing vertices that satisfy the condition: $ (2,3,4) $ and $ (3,2,4) $ .
### Constraints
- $ 1 \leq T \leq 125000 $
- $ 2 \leq N \leq 250000 $
- $ 1 \leq P_v < v $ $ (2 \leq v \leq N) $
- The sum of $ N $ over all test cases in a single input is at most $ 250000 $ .
- All input values are integers.