Task
We have a tree with nodes, where each node contains a natural number, and the values found in the nodes form a permutation of the set .
This tree has its root at node and is special in that each node has at most children.
The following process is defined:
- A sequence is initialized as empty.
- A pawn is placed on node , which is marked as visited. The value of node is added to the sequence.
- As long as there are unvisited nodes, the pawn moves to a neighboring node of its current position, even if that node has already been visited.
- If the node the pawn arrives at has not been visited, it is marked as visited and the node's value is added to the sequence.
- The pawn can only move to an already visited node if all nodes in the subtree of the current node have been visited.
It is observed that after this process, the resulting sequence will be a permutation. How many processes will result in a permutation with an odd number of inversions?
Two processes are considered different if the resulting sequence is different. The answer should be calculated modulo .
Input data
The first line contains , the number of nodes in the tree. The second line contains values, representing the values of the nodes. The next lines contain two values each, representing the endpoints of the tree's edges.
Output data
On the first line, print the number of resulting sequences, modulo .
Constraints and clarifications
- Each node has at most children.
- The answer is printed modulo .
- For tests worth points, .
- For tests worth more points, .
- For tests worth more points, each node has at most children.
- For tests worth more points, each node has at most children.
Example 1
stdin
5
3 1 4 2 5
1 2
1 3
3 4
3 5
stdout
2
Explanation
The following sequences can be obtained from the processes:
- — inversions
- — inversions
- — inversions
- — inversions
Note that the sequence is not valid.
The pawn would have the following path: .
The pawn is not allowed to move from node to node because there are still unvisited nodes in the subtree of node .
Example 2
stdin
17
8 12 4 7 3 5 1 17 11 2 6 9 10 14 16 15 13
1 2
1 3
1 4
2 8
2 9
2 10
2 11
4 5
4 6
6 7
10 12
10 13
13 14
13 15
13 16
13 17
stdout
6912