#P12355. 「HCOI-R2」巡回演出

    ID: 11651 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>洛谷原创O2优化组合数学Catalan 数生成函数洛谷月赛Prüfer 序列

「HCOI-R2」巡回演出

Description

The country of HCOI consists of nn cities, which is abstracted as a rooted tree with nn nodes, where the capital is the root rr. Little A plans to start from rr and traverse all cities for a performance.

Specifically, let xx be Little A’s current position, and define a sequence ss as follows:

  • Initially, x=rx = r and s=[r]s = [r].
  • If xx has unvisited children, arbitrarily choose one child yy, append yy to the end of ss, and move to yy.
  • If no such yy exists: If x=rx = r, the performance ends. Otherwise, move to xx's parent.

The final sequence ss is called a traversal method of TT. Different choices of children yield multiple traversal methods. Note that traversal methods are essentially DFS orders of TT.

Little A's assistant provides a fee table {wn1}\{w_{n-1}\}. The revenue w(T,s)w(T, s) of the tour is defined as:

  • For i[2,n]i \in [2, n], Little A traverses the simple path si1sis_{i-1} \to s_i on TT (excluding si1s_{i-1}, including sis_i).
  • Each time a node is visited, if it is the ii-th time of arrival, Little A receives wiw_i gold coins as payment.
  • w(T,s)w(T, s) is the total number of gold coins Little A collects.

The assistant also provides the traversal method {sn}\{s_n\} for this performance. Little A tree TT is defined as valid if and only if {sn}\{s_n\} is one of its traversal methods. You need to compute the sum of w(T,s)w(T, s) over all distinct valid trees TT, modulo 998244353998244353.

Two trees TT and TT' are considered distinct if they have different roots or there exists an edge present in one tree but not the other.

Input Format

Each single test case has multiple sets of test data.

The first line contains a positive integer TT representing the number of test cases. For each test case:

The first line contains a positive integer nn, indicating the number of cities in HCOI.

The second line contains nn positive integers s1,s2,,sns_1, s_2, \cdots, s_n, representing the traversal method of A’s performance.

The third line contains n1n-1 non-negative integers w1,w2,,wn1w_1, w_2, \cdots, w_{n-1}, representing the fee table for the performance.

Output Format

For each test case, output a single line of integer representing the sum of all valid TT's w(T)w(T) modulo 998244353998244353.

7
2
1 2
1
3
1 2 3
1 2
4
1 2 3 4
1 2 3
5
1 3 5 2 4
1 3 2 1
6
6 2 3 4 1 5
3 2 4 7 4
7
1 3 2 4 5 6 7
12 32 84 27 83 11
8
1 7 3 2 8 4 6 5
11 45 14 19 19 8 10
2
10
42
182
1240
41336
124348
2
9
1 2 3 4 5 6 7 8 9
18 48 72 49 83 59 74 80
12
1 12 2 4 3 6 5 7 8 9 11 10
120 938 283 920 462 748 932 784 178 904 442
787978
522215784
1
4
1 2 3 4
1 0 0
20

Hint

Constraints

This problem uses subtasks.

Subtask n\boldsymbol{n\le} n\boldsymbol{\sum n\le} Special Property Score
00 44 10610^6 A 55
11 88 4040 1515
22 1212 6060 None 1010
33 5050 200200
44 100100 500500
55 10310^3 5×1035\times 10^3 B
66 None 55
77 10510^5 2×1052\times 10^5 B
88 None 1515
99 5×1055\times10^5 10610^6
  • Special Property A: For all i[1,n]i \in [1, n], si=is_i = i.
  • Special Property B: w1=1w_1 = 1, and for all i[2,n1]i \in [2, n-1], wi=0w_i = 0.

For all test cases, it is guaranteed that 1T1051 \le T \le 10^5, 2n5×1052 \le n \le 5 \times 10^5, n106\sum n \le 10^6, 0wi<9982443530 \le w_i < 998244353, {sn}\{s_n\} is a permutation of 1,2,,n1,2,\cdots,n.