#P9437. 『XYGOI round1』一棵树
『XYGOI round1』一棵树
Background
Java brought a tree to the problem-setting group today, but the unreasonable MX took it for himself.
Problem Description
MX has a tree with nodes, and each node has a number on it.
Define the weight of a path as the number obtained by writing down, in order, all the numbers on the nodes along the shortest path from to . For example, if the path passes through four nodes labeled with numbers in this order, then the weight of this path is .
MX wants to know the sum of the weights of all paths in this tree, i.e., .
The answer may be very large. Output it modulo .
Input Format
The first line contains a positive integer .
The second line contains non-negative integers .
The third line contains positive integers. The -th number indicates that there is an edge between node and node . It is guaranteed that .
Output Format
Output one integer in a single line, representing the answer modulo .
3
1 2 3
1 2
538
3
5 21 0
1 2
6418
4
1 2 3 4
1 2 2
1900
6
10 23 16 3 125 1
1 1 1 1 1
7680868
Hint
Sample Explanation
Explanation for sample 1: .
Explanation for sample 2: .
Constraints
This problem uses bundled testdata.
Let .
| Subtask | Points | Special Properties | ||
|---|---|---|---|---|
| 0 | 5 | |||
| 1 | 15 | |||
| 2 | ||||
| 3 | ||||
| 4 | 50 |
For of the testdata, and .
Translated by ChatGPT 5