#P960E. Alternating Tree
Alternating Tree
Description
Given a tree with nodes numbered from to . Each node has an associated value .
If the simple path from to consists of nodes namely , then its alternating function is defined as . A path can also have edges, i.e. .
Compute the sum of alternating functions of all unique simple paths. Note that the paths are directed: two paths are considered different if the starting vertices differ or the ending vertices differ. The answer may be large so compute it modulo .
The first line contains an integer — the number of vertices in the tree.
The second line contains space-separated integers () — values of the nodes.
The next lines each contain two space-separated integers and denoting an edge between vertices and . It is guaranteed that the given graph is a tree.
Print the total sum of alternating functions of all unique simple paths modulo .
Input
The first line contains an integer — the number of vertices in the tree.
The second line contains space-separated integers () — values of the nodes.
The next lines each contain two space-separated integers and denoting an edge between vertices and . It is guaranteed that the given graph is a tree.
Output
Print the total sum of alternating functions of all unique simple paths modulo .
Note
Consider the first example.
A simple path from node to node : has alternating function equal to .
A simple path from node to node : has alternating function equal to .
A simple path from node to node : has alternating function .
A simple path from node to node has a single node , so .
Similarly, , , , , , , , , , , , . So the answer is .
Similarly which sums upto 40.