cesta — COCI CONTEST_#3
Task type: batch | Time limit: 3 s | Memory limit: 512 MB | Difficulty: 18
Algorithms: dynamic programming, linear algebra, tree, modular arithmetic, expected value

Task Slučajna Cesta
3 seconds / 512 MiB / 110 points

Task Slučajna Cesta
Vito lives in a city with *n* parks labeled from 1 to *n*. The parks are connected with *n* - 1 roads such that there is a path between any two pairs of parks. Every park has some beauty value, beauty value of *i*-th park is *vi*.

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 i-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.

## **Input**

In the first line there is an integer *n* (2 ≤ *n* ≤ 106), which denotes the number of parks.

In the second line there are *n* - 1 integers *pi* (1 ≤ *pi* < *i*), which denote a road between the (*i* + 1)-th park and *pi*-th park.

In the third line there are *n* integers *vi* (0 ≤ *vi* ≤ 106), where *vi* denotes the beauty of *i*-th park.

# **Output**

If expected beauty of Vito's journey which starts at *i*-th park is *a b* for integers *a* and *b*, then in *i*-th line of output print *ab*-1 (mod 109 + 7) where *b*-1 is modular inverse of *b* (mod 109 + 7).

### **Scoring**

| Subtask | Points | Constraints |
|---|---|---|
| 1 | 10 | \( n \le 10 \) |
| 2 | 30 | \( n \le 1000 \) |
| 3 | 30 | In sequence pi no value is present more than 2 times. |
| 4 | 40 | No additional constraints. |

If your program, on some test, outputs the first line correct, but outputs a wrong answer in the following lines, **it will receive 50% of points for that test.**

The number of points in a subtask corresponds to the least number of points achieved by some test in that subtask.

## **Probni primjeri**

| input | input | input |
|---|---|---|
| 2<br>1<br>2 1 | 3<br>1 1<br>8 8 8 | 11<br>1 1 1 2 3 4 1 2 6 2<br>1 1000 5 3 18 200 8 9 0 2 2 |
| output | output | output |
| 500000006<br>2 | 14<br>14<br>14 | 968750272<br>610352580<br>450521029<br>536458466<br>199219275<br>662760680<br>190972315<br>90277951<br>824219264<br>941840425<br>532552597 |

#### **Clarification of the first example:**

The expected beauty of a journey starting at the first park is 2.5 (mod 109+7) = 5 / 2 (mod 109+7) = 5 * 2-1 (mod 109 + 7) = 5 * 500000004 (mod 109 + 7) = 500000006 (mod 109 + 7) and starting from the second park it is 2.

#### **Clarification of the second example:**

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

Starter code: