|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|1 초||512 MB||0||0||0||0.000%|
One morning, Răzvăran decided to invite his two friends, Matthew and Peter, to go for a walk in a well-known public garden. This garden has the shape of a tree with n nodes (indexed from 1 to n) and between n-1 pairs of nodes there are paths which measure 10 meters. In each i node, there is an i spring. The friends only accept the invitation if the walk respects the following rules: Matthew, a pretentious young man, wants to get to all of the springs walking as few meters as possible and Peter, wanting to rise up to the challenge, says that he wants to visit m springs in a pre-established order: P1, P2, … , Pm. Răzvăran now wonders in how many ways they can walk, knowing that both the entrance and the exit are at the first spring, which is at the same time the root of the tree.
Determine the number of ways in which the three friends can walk. This number must be printed modulo 109+7.
On the first line of the input there are two positive integers, n and m, which represent the number of springs of the public garden and the number of springs Peter wants to visit. The next line contains n-1 numbers T2, T3, …, Tn representing the parent array corresponding to the tree. On the next line there are m distinct numbers P1, P2, …, Pm.
On the first line of the output you should print the required number.
7 3 1 1 2 2 2 3 5 4 7
The correct ways the three friends can walk are:
Moreover, three invalid walks are: