#P10013. [集训队互测 2023] Tree Topological Order Counting

[集训队互测 2023] Tree Topological Order Counting

Problem Description

Given a rooted tree with nn nodes, where node 11 is the root, let the parent of uu be faufa_u. You are also given a weight sequence bb of length nn.

A permutation aa of length nn is called a valid topological order of this tree if and only if 2un,au>afau\forall 2 \le u \le n, a_u > a_{fa_u}.

For each node uu, define f(u)f(u) as the sum of baub_{a_u} over all valid topological orders of this tree.

Now, for 1un1 \le u \le n, compute f(u)mod109+7f(u) \bmod 10^9+7.

Input Format

The first line contains an integer nn, the number of nodes in the tree.

The second line contains n1n-1 integers. The ii-th integer denotes fai+1fa_{i+1}, describing the structure of the tree.

The third line contains nn integers. The ii-th integer denotes bib_i, describing the weight sequence.

Output Format

Output one line with nn integers. The ii-th integer denotes f(i)mod109+7f(i) \bmod 10^9+7.

5
1 1 3 2
3 5 4 4 1

18 27 27 15 15 

5
1 1 3 1
1 2 3 4 5

12 42 32 52 42

Hint

Subtask nn \le Special Constraint Score
11 1010 None 55
22 2020 1010
33 100100 1515
44 350350
55 30003000 A
66 None 2020
77 50005000

Special constraint A: 1in,bi=i\forall 1 \le i \le n, b_i = i.

Constraints for all testdata: 2n50002 \le n \le 5000, 1fai<i1 \le fa_i < i, 0bi<109+70 \le b_i < 10^9+7.

Translated by ChatGPT 5