题目描述
Problem Description
ButCoder Inc. is a startup company primarily engaged in the development and operation of the programming contest site "ButCoder".
ButCoder Inc. has $ N $ employees including the president. Each employee, except for the president, has exactly one direct supervisor. Each employee is assigned a unique employee number from $ 1 $ to $ N $, and the employee with employee number $ i $ is referred to as employee $ i $. The president is employee $ 1 $, and the direct supervisor of employee $ i $ $(2 \leq i \leq N)$ is employee $ b_i $ $(1 \leq b_i < i)$.
In order to take a group photo of all ButCoder employees, everyone gathered in the large hall of the ButCoder headquarters. This hall is so large that all $ N $ employees can line up in a single row. However, they are having trouble deciding in what order to line up. For some reason, everyone except the president wants to avoid standing next to their direct supervisor.
How many ways are there for the $ N $ employees to line up such that their wishes are fulfilled? Find the number of such arrangements modulo $ 10^9+7 $.
Input Format
The input is given from the standard input in the following format:
$ N $ $ b_2 $ $ b_3 $ $ : $ $ b_N $
Output Format
Output the number of ways for the $ N $ employees to line up such that no employee (except the president) stands next to their direct supervisor, modulo $ 10^9+7 $.
Sample Input and Output
Sample Input #1
4
1
2
3
Sample Output #1
2
Sample Input #2
3
1
2
Sample Output #2
0
Sample Input #3
5
1
1
3
3
Sample Output #3
8
Sample Input #4
15
1
2
3
1
4
2
7
1
8
2
8
1
8
2
Sample Output #4
97193524
Notes/Hints
Constraints
- $2 \leq N \leq 2000$
- $1 \leq b_i < i$ $(2 \leq i \leq N)$
Sample Explanation 1
Here, the employee-supervisor relationship is $1 \rightarrow 2 \rightarrow 3 \rightarrow 4$. The following $ 2 $ arrangements satisfy everyone's wishes: - $ 2, 4, 1, 3 $ - $ 3, 1, 4, 2 $ Note that the latter is a mirror image of the former, but these are counted separately.
Sample Explanation 2
The employee-supervisor relationship is $1 \rightarrow 2 \rightarrow 3$. No matter how the three employees line up, at least one of the wishes of employees $ 2 $ and $ 3 $ will be violated, so the answer is $ 0 $ ways.
Sample Explanation 3
The employee-supervisor relationship is as shown in the following diagram:
The following $ 8 $ arrangements satisfy everyone's wishes: - $ 1, 4, 5, 2, 3 $ - $ 1, 5, 4, 2, 3 $ - $ 3, 2, 4, 1, 5 $ - $ 3, 2, 4, 5, 1 $ - $ 3, 2, 5, 1, 4 $ - $ 3, 2, 5, 4, 1 $ - $ 4, 1, 5, 2, 3 $ - $ 5, 1, 4, 2, 3 $
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分