#P14016. [ICPC 2024 Nanjing R] 拓扑

    ID: 15799 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>数学2024拓扑排序组合数学逆元ICPC南京

[ICPC 2024 Nanjing R] 拓扑

Problem Description

You are given a tree consisting of nn vertices, rooted at vertex 11. It is guaranteed that every vertex has a smaller index than all of its children. A topological order of this tree is a permutation p1,p2,…,pnp_1,p_2,\dots,p_n of nn that satisfies the following constraint: For all 1≤i<j≤n1\leq i<j\leq n, vertex pjp_j is not the parent of vertex pip_i.

For each 1≤i≤n1 \le i \le n, calculate the number of topological orders of the given tree satisfying pi=ip_i=i, modulo 998 244 353998\,244\,353.

Input Format

There is only one test case in each test file.

The first line contains an integer nn (2≤n≤5 0002\leq n\leq 5\,000), denoting the number of vertices of the tree.

The second line contains (n−1)(n-1) integers f2,f3,…,fnf_2,f_3,\dots,f_n (1≤fi<i1\leq f_i< i), where fif_i is the parent of vertex ii.

Output Format

Output one line containing nn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n separated by a space, where aia_i is the number of topological orders satisfying pi=ip_i=i, modulo 998 244 353998\,244\,353.

4
1 1 2
3 2 1 2
9
1 1 2 2 3 3 4 5
672 420 180 160 152 108 120 170 210

Hint

For the first sample test case, all topological orders of the tree are {1,2,3,4}\{1, 2, 3, 4\}, {1,3,2,4}\{1, 3, 2, 4\} and {1,2,4,3}\{1, 2, 4, 3\}. There are 33 of them satisfying p1=1p_1 = 1, 22 of them satisfying p2=2p_2 = 2, 11 of them satisfying p3=3p_3 = 3, and 22 of them satisfying p4=4p_4 = 4.