#P7422. 「PMOI-2」城市
「PMOI-2」城市
Problem Description
Country has cities and undirected roads. City is the capital, and any two cities can reach each other through roads.
Now Country is going to hold ION in the capital. To build the contest venue, each city needs to provide raw materials to the capital. City can provide raw material of type . Each city will have a truck departing from that city and going to the capital along a simple path. If the truck starting from city must pass through city , then we say that city lies on the unavoidable path from city to the capital. If for cities , any simple path from to and any simple path from to have no common edges, then we say that cities and are independent of each other with respect to city .
Let be the number of -element sets that satisfy all of the following conditions:
-
For any with , city lies on the unavoidable path from city to the capital, and the material supplied by city is different from that of city .
-
For any , and are independent with respect to , and the raw materials supplied by and are the same.
Define the attractiveness of holding ION as , where is a given constant.
Now, as the leader of Country , you want to know the attractiveness of this ION.
Since the answer may be very large, please output the answer modulo .
Input Format
The first line contains three integers .
The second line contains integers. The -th number is the type of raw material supplied by city .
The next lines each contain two integers , indicating a road in Country . It is guaranteed that there are no multiple edges and no self-loops. ()
Output Format
Output one integer, the answer.
7 7 2
1 2 3 3 1 1 2
1 2
2 3
2 4
3 4
3 5
3 6
4 7
12
Hint
[Sample Explanation]
In the sample, the map of Country is as follows:

In the table below, the entry in row and column represents .
So the attractiveness is .
[Constraints]
This problem uses bundled testdata.
- Subtask 1 (10 pts): .
- Subtask 2 (10 pts): .
- Subtask 3 (15 pts): .
- Subtask 4 (15 pts): the graph is guaranteed to be a tree.
- Subtask 5 (15 pts): .
- Subtask 6 (15 pts): .
- Subtask 7 (20 pts): no special constraints.
For of the data: , $1 \le M \le \min\left(10^6, \frac{N \times (N - 1)}{2}\right)$, , .
Warm reminder: The input is large, so please use a fast input method.
Translated by ChatGPT 5