On his holiday, Josh finds himself in a kingdom with villages, which he labels from to . The kingdom is connected by roads with equal length, with the -th road connecting villages and , such that each village can be reached from every other village. The distance between two villages is the minimum number of roads needed to travel between them.
Josh's tour guide tells him that there are also some positive number of tribes, each denoted with a positive integer, and each having a headquarter in exactly one village. No two tribes have the same headquarter. Additionally, each village is occupied by exactly one tribe - the tribe whose headquarters are the shortest distance away from the village. If there are multiple tribes with headquarters being the shortest distance away from the village, the village could be occupied by any of those tribes.
After exploring each village, Josh determines that the -th village is given occupied by tribe , but he doesn't know where any of the headquarters are. He additionally notes that for any two villages occupied by the same tribe , it is possible to travel between the two villages along the roads, only travelling via villages occupied by tribe .
Josh would like to know the number of different possible states of the kingdom. Two kingdoms states are considered different if there exists a village satisfying one of the two conditions below:
- Both kingdom states have a headquarter at that village, but the headquarters belong to different tribes.
- One kingdom state has a headquarter at that village, and the other kingdom state doesn't.
Since this number might be very large, output it modulo . Note that there might be no possible states, in which case the answer is .
Note: it is not required that the tribes present are labelled from onwards. For example, it is possible that tribe occupies the entire kingdom.
Constraints
All villages can be reached from all other villages by travelling along the roads.
For any two villages occupied by the same tribe , it is possible to travel between the two villages along the roads, only travelling via villages occupied by tribe .
Subtask 1 [15%]
Subtask 2 [15%]
Subtask 3 [70%]
No additional constraints.
Input Specification
The first line contains two space-separated integers, and .
The second line contains space-separated integers, .
The -th of the following lines contains two space-separated integers, .
Output Specification
Output the number of possible kingdom states modulo .
Sample Input
5 10
1 1 2 2 2
1 2
1 3
3 4
3 5
Sample Output
6
Explanation
For this kingdom, tribe could have its headquarters in villages or , and tribe could have its headquarters in village , or . Furthermore, all of these combinations yield a valid kingdom state. For example, if tribe has its headquarters in village , and tribe has its headquarters in village , then:
- Village is equidistant to both headquarters, so it could feasibly be occupied by tribe .
- Village is closest to the headquarters located in village , which belongs to tribe .
- Villages , and are closest to the headquarters located in village , which belongs to tribe .
Comments