## COCI '23 Contest 3 #5 Slučajna Cesta

View as PDF

Points: 15 (partial)
Time limit: 3.0s
Memory limit: 512M

Problem types

Vito lives in a city with parks labeled from to . The parks are connected with roads such that there is a path between any two pairs of parks. Every park has some beauty value; the beauty value of the -th park is .

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, and red snakes attack everyone traveling from a park with a higher label to a park with a lower one. 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 -th park?

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

#### Input Specification

In the first line there is an integer , which denotes the number of parks.

In the second line there are integers , which denote a road between the -th park and -th park.

In the third line there are integers , where denotes the beauty of the -th park.

#### Output Specification

If the expected beauty of Vito's journey which starts at the -th park is for integers and , then in the -th line of output, print where is modular inverse of .

#### Scoring

1 10
2 30
3 30 In the sequence , no value is present more than times.

If your program, on some test, outputs the first line correct, but outputs a wrong answer in the following lines, it will receive of the 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.

#### Sample Input 1

2
1
2 1

#### Sample Output 1

500000006
2

#### Explanation for Sample 1

The expected beauty of a journey starting at the first park is and starting from the second park it is .

#### Sample Input 2

3
1 1
8 8 8

#### Sample Output 2

14
14
14

#### Explanation for Sample 2

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

#### Sample Input 3

11
1 1 1 2 3 4 1 2 6 2
1 1000 5 3 18 200 8 9 0 2 2

#### Sample Output 3

968750272
610352580
450521029
536458466
199219275
662760680
190972315
90277951
824219264
941840425
532552597