QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#7792. Tree Topological Order Counting

Statistics

Given a rooted tree with $n$ nodes, where $1$ is the root and $fa_u$ denotes the parent of $u$. You are also given a weight sequence $b$ of length $n$.

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

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

For each $1 \le u \le n$, calculate $f(u) \bmod 10^9+7$.

Input

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

The second line contains $n-1$ integers, where the $i$-th integer represents $fa_{i+1}$, describing the structure of the tree.

The third line contains $n$ integers, where the $i$-th integer represents $b_i$, describing the weight sequence.

Output

A single line containing $n$ integers, where the $i$-th integer represents $f(i) \bmod 10^9+7$.

Examples

Input 1

5
1 1 3 2 
3 5 4 4 1

Output 1

18 27 27 15 15

Input 2

5
1 1 3 1
1 2 3 4 5

Output 2

12 42 32 52 42

Subtasks

Subtask $n \le$ Special Constraints Score
$1$ $10$ None $5$
$2$ $20$ None $10$
$3$ $100$ None $15$
$4$ $350$ None $15$
$5$ $3000$ A $15$
$6$ $3000$ None $20$
$7$ $5000$ None $20$

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

For all data: $2 \le n \le 5000$, $1 \le fa_i < i$, $0 \le b_i < 10^9+7$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.