#P1254E. Send Tree to Charlie

    ID: 2117 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>combinatoricsdfs and similardsutrees*3300

Send Tree to Charlie

Description

Christmas was knocking on the door, and our protagonist, Bob, was preparing a spectacular present for his long-time second best friend Charlie. As chocolate boxes are lame, he decided to decorate a tree instead. Bob's tree can be represented as an undirected connected graph with nn nodes (numbered 11 to nn) and n1n-1 edges. Initially, Bob placed a decoration with label ii on node ii, for each 1in1 \le i \le n. However, as such a simple arrangement is lame, he decided to shuffle the decorations a bit. Formally, Bob did the following steps:

  • First, he listed the n1n-1 edges in some order.
  • Then, he considered the edges one by one in that order. For each edge (u,v)(u, v), he swapped the decorations of node uu with the one of node vv.

After finishing, Bob seemed satisfied with the arrangement, so he went to sleep.

The next morning, Bob wakes up only to find out that his beautiful arrangement has been ruined! Last night, Bob's younger brother Bobo dropped some of the decorations on the floor while he was playing with the tree. Fortunately, no decorations were lost, so Bob can repair the tree in no time. However, he completely forgets how the tree looked like yesterday. Therefore, given the labels of the decorations still on the tree, Bob wants to know the number of possible configurations of the tree. As the result can be quite large, Bob will be happy if you can output the result modulo 10000000071000000007 (109+710^9+7). Note that, it is possible that there exists no possible configurations.

The first line contains a single integer nn (2n5000002 \leq n \leq 500\,000) — the number of nodes.

Each of the next n1n - 1 lines contains two integers uu and vv (1u,vn1 \leq u, v \leq n), denoting that there is an edge connecting two nodes uu and vv. It is guaranteed that the given edges form a tree.

The last line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ain0 \le a_i \le n). For each ii, ai=0a_i = 0 means that the decoration of node ii has been dropped on the floor. Otherwise, aia_i is the label of the decoration of node ii. It is guaranteed that no label appears more than once.

Output the number of possible configurations modulo 10000000071000000007 (109+710^9+7).

Input

The first line contains a single integer nn (2n5000002 \leq n \leq 500\,000) — the number of nodes.

Each of the next n1n - 1 lines contains two integers uu and vv (1u,vn1 \leq u, v \leq n), denoting that there is an edge connecting two nodes uu and vv. It is guaranteed that the given edges form a tree.

The last line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ain0 \le a_i \le n). For each ii, ai=0a_i = 0 means that the decoration of node ii has been dropped on the floor. Otherwise, aia_i is the label of the decoration of node ii. It is guaranteed that no label appears more than once.

Output

Output the number of possible configurations modulo 10000000071000000007 (109+710^9+7).

Samples

样例输入 1

4
3 4
2 4
4 1
0 4 0 0

样例输出 1

2

样例输入 2

5
1 2
2 4
3 4
5 4
0 0 0 0 0

样例输出 2

12

样例输入 3

3
1 2
1 3
1 0 0

样例输出 3

0

Note

In the first example, the possible configurations of the tree are [2,4,1,3][2, 4, 1, 3] and [3,4,2,1][3, 4, 2, 1].

In the second example, note that while there are 4!=244! = 24 possible permutations of the edges, each of them results in a possible configuration, there are only 1212 different configurations.

In the third example, it is easy to see that the decoration 11 cannot stay at node 11 after the swaps.