PivotOJ

Slučajna Cesta

시간 제한: 3000ms메모리 제한: 1024MB출처: COCI 2023-2024BOJ 31273

문제

Vito lives in a city with nn parks labeled from 11 to nn. The parks are connected with n1n - 1 roads such that there is a path between any two pairs of parks. Every park has some beauty value, beauty value of ii-th park is viv_i.

Last night Vito decided to wander around the city in such a way that after he visits a park he chooses a random road with equal probability and visits a park to which that road leads. But before he started his journey he looked through the window of his skyscraper and saw that on every road there is either a blue or a red snake. Blue snakes attack all people traveling from the park with a lower label to a park with a higher one, a red snakes attack everyone traveling from a park with higher label to lower. As Vito doesn’t want to get attacked by a snake he decided to change his plans by considering only roads on which he will not get attacked by a snake when choosing a random road. Since he likes long walks he will not stop on his journey until there is at least one road he can safely pass.

And while Vito walks down the stairs of his skyscraper he completely forgot on which road is red or blue snake so he wonders: If on every road there is an equal probability of a blue or a red snake, what is the expected beauty of my journey which starts in the ii-th park?

Beauty of path is the sum of beauties of parks visited on that journey. Expected beauty of journey is defined as the sum of product of beauty of a path and probability Vito takes that path, for every possible path.

입력

In the first line there is an integer nn (2 ≤ n ≤ 10^6), which denotes the number of parks.

In the second line there are n1n - 1 integers pip_i (1 &le; p_i < i), which denote a road between the (i+1)(i + 1)-th park and pip_i-th park.

In the third line there are nn integers viv_i (0 &le; v_i &le; 10^6), where viv_i denotes the beauty of ii-th park.

출력

If expected beauty of Vito’s journey which starts at ii-th park is ab\frac{a}{b} for integers aa and bb, then in ii-th line of output print ab1(mod109+7)ab^{-1} \pmod {10^9 + 7} where b1b^{-1} is modular inverse of b(mod109+7)b \pmod {10^9 + 7}.

힌트

Clarification of the first example:

The expected beauty of a journey starting at the first park is 2.5(mod109+7)=52(mod109+7)=521(mod109+7)=5500000004(mod109+7)=500000006(mod109+7)2.5 \pmod {10^9 + 7} = \frac{5}{2} \pmod {10^9 + 7} = 5 \cdot 2 ^{-1} \pmod {10^9 + 7} = 5 \cdot 500000004 \pmod {10^9 + 7} = 500000006 \pmod {10^9 + 7} and starting from the second park it is 22.

Clarification of the second example:

Probability that both snakes are red is 14\frac{1}{4} and in that case if Vito starts at the first park he randomly chooses which road he will take.

예제

예제 1

입력
2
1
2 1
출력
500000006
2

예제 2

입력
3
1 1
8 8 8
출력
14
14
14

예제 3

입력
11
1 1 1 2 3 4 1 2 6 2
1 1000 5 3 18 200 8 9 0 2 2
출력
968750272
610352580
450521029
536458466
199219275
662760680
190972315
90277951
824219264
941840425
532552597
코드를 제출하려면 로그인하세요.