#P10053. [CCO 2022] Bi-ing Lottery Treekets

[CCO 2022] Bi-ing Lottery Treekets

Problem Description

In a parallel universe, everyone got a perfect score on CCO. Therefore, Troy needs to use a lottery to choose the winner. Each contestant will pick some numbers to create a ticket. A ticket is an array of size NN, indexed from 11 to NN, where each element is a number from 00 to KK.

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

Each ball has a designated drop node, where it will be dropped. When a ball is dropped into an empty node, or enters an empty node, one of the following three situations happens:

  1. If all children of the current node are occupied by balls (or if a node has no children), then the current ball will stay at the current node. That is, it remains there and will not move anymore.
  2. If the current node has exactly one empty child, then the current ball will move to that child.
  3. If the current node has two empty children, and if the current ball was just dropped, it may move left or right. Otherwise, it will continue moving in the same direction as before.

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

If all KK balls are dropped successfully, then the final positions of the balls determine the winning ticket. The ii-th element of the winning ticket is the label of the ball that stops at node ii, or 00 if no ball stops at node ii.

Troy wants to know the number of possible winning tickets. Since the answer can be very large, you only need to output it modulo 109+710^9+7.

Input Format

The first line contains two integers NN and KK separated by spaces, representing the number of nodes in the binary tree and the number of balls.

The second line contains KK integers separated by spaces, where the ii-th integer is the designated drop node of ball ii.

The last NN lines each contain two integers separated by spaces. The ii-th of these lines contains LiL_{i} and RiR_{i}, representing the left child and right child of node ii, where 00 means the child does not exist.

Output Format

Output the number of winning tickets modulo 109+710^{9}+7.

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

Hint

Explanation for Sample 1

Consider the case where ball 11 is dropped first. If ball 11 moves left, then it will stop at node 22. Afterwards, ball 22 is dropped and may stop at node 44 or 55. If ball 11 moves right, it will stop at node 55. Then ball 22 will stop at node 44.

Consider the case where ball 22 is dropped first. Ball 22 may move left or right and stop at node 44 or 55, respectively. Then, if ball 11 moves left after being dropped, it will stop at node 22. However, if ball 11 moves right, it will stop at node 44 or 55, depending on which position ball 22 did not occupy.

The possible winning tickets are [0,1,0,2,0][0,1,0,2,0], [0,1,0,0,2][0,1,0,0,2], [0,0,0,1,2][0,0,0,1,2], and [0,0,0,2,1][0,0,0,2,1].

Explanation for Sample 2

This sample satisfies the condition of the second subtask.

Ball 33 must be dropped first, because if ball 11 or ball 22 is dropped before ball 33, it would stop at node 44, which would prevent ball 33 from being dropped.

Therefore, we can drop ball 33 first, then ball 22, and finally ball 11, or we can drop ball 33 first, then ball 11, and finally ball 22. The corresponding winning tickets are [0,1,2,3][0,1,2,3] and [0,2,1,3][0,2,1,3].

Constraints

For all testdata, 1N,K40001\leq N,K \leq 4000.

Subtask ID Points Range of NN Range of KK Additional constraints
11 1212 1N121 \leq N \leq 12 1K61 \leq K \leq 6 None
22 1616 1N40001 \leq N \leq 4000 1K40001 \leq K \leq 4000 All nodes have no left child
33 3636 The KK drop nodes are all distinct
44 None

Translated by ChatGPT 5