Bi-ing Lottery Treekets

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

In a parallel universe, everyone scored perfect on the CCO. As a result, Troy needs to pick the winner based on a lottery. Each contestant will choose numbers to create a ticket. A ticket is an array of size NN indexed from 11 to NN where each entry is a number from 00 to KK.

The winning ticket is determined by dropping KK balls (numbered from 11 to KK) in a random sequence into a rooted binary tree. The tree has NN nodes (numbered from 11 to NN) and is rooted at node 11.

Each ball has a designated drop node that it will drop at. When a ball is dropped at an unoccupied node or enters an unoccupied node, one of three things happens:

  1. If all of the current node's children are occupied by balls (or if a node has no children), the current ball rests at the current node. That is, it remains there and does not move again.
  2. If the current node only has one unoccupied child, the current ball will move to this child.
  3. If the current node has two unoccupied children, and if the current ball was just dropped, it could go either left or right. Otherwise, it will continue in the direction of its previous movement.

If all KK balls cannot be dropped, a winning ticket is not determined. This happens when a ball is dropped and its drop node is occupied by another ball.

If all KK balls have been dropped, the balls' resting positions determine the winning lottery ticket. The ithi^\text{th} entry of the winning lottery ticket is the number of the ball that rests at node ii or 00 if no ball rests at node ii.

Troy would like to know the number of possible winning tickets (which could be zero).

입력

The first line contains two space-separated integers NN and KK, denoting the number of nodes in the binary tree and the number of balls, respectively.

The next line contains KK space-separated integers, where the ithi^\text{th} integer denotes the designated drop node of the ball numbered ii.

The last NN lines each contain two space-separated integers. The ithi^\text{th} line contains L_iL\_i and R_iR\_i denoting the ithi^\text{th} node's left and right child, respectively, where 00 means no such child exists.

출력

Output the remainder of the number of winning lottery tickets divided by 109+710^9 + 7.

힌트

A binary tree is a set of nodes that are either empty or a root node with a left subtree and a right subtree, both of which are binary trees. Given a node xx, if its left subtree is not empty, then the root of that subtree is called the left child of xx. Similarly, given a node xx, if its right subtree is not empty, then the root of that subtree is called the right child of xx.