Problem:
Score : 1000 points

Problem Statement

ButCoder Inc. is a startup company whose main business is the development and operation of the programming competition site "ButCoder".

There are N members of the company including the president, and each member except the president has exactly one direct boss. Each member has a unique ID number from 1 to N, and the member with the ID i is called Member i. The president is Member 1, and the direct boss of Member i (2 ≤ i ≤ N) is Member b_i (1 ≤ b_i < i).

All the members in ButCoder have gathered in the great hall in the main office to take a group photo. The hall is very large, and all N people can stand in one line. However, they have been unable to decide the order in which they stand. For some reason, all of them except the president want to avoid standing next to their direct bosses.

How many ways are there for them to stand in a line that satisfy their desires? Find the count modulo 10^9+7.

Constraints

2 ≤ N ≤ 2000

1 ≤ b_i < i (2 ≤ i ≤ N)

Input

Input is given from Standard Input in the following format:

N
b_2
b_3
:
b_N

Output

Print the number of ways for the N members to stand in a line, such that no member except the president is next to his/her direct boss, modulo 10^9+7.

Sample Input 1

4
1
2
3

Sample Output 1

2

Below, we will write A → B to denote the fact that Member A is the direct boss of Member B.

In this case, the hierarchy of the members is 1 → 2 → 3 → 4. There are two ways for them to stand in a line that satisfy their desires:

2, 4, 1, 3

3, 1, 4, 2

Note that the latter is the reverse of the former, but we count them individually.

Sample Input 2

3
1
2

Sample Output 2

0

The hierarchy of the members is 1 → 2 → 3. No matter what order they stand in, at least one of the desires of Member 2 and 3 is not satisfied, so the answer is 0.

Sample Input 3

5
1
1
3
3

Sample Output 3

8

The hierarchy of the members is shown below:

There are eight ways for them to stand in a line that satisfy their desires:

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

Sample Input 4

15
1
2
3
1
4
2
7
1
8
2
8
1
8
2

Sample Output 4

97193524

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

#define int ll
const int mod = 1e9+7;
const int N = 2007;
const int inv2 = (mod+1)/2;

int n;

VI G[N];

int sub[N];
int dp[2][N][N][3][2]; //[korzen][ile skladowych][stopien korzenia][parzystosc liczby krawedzi]
int c[N];

void dfs(int v)
{
	sub[v] = 1;

	dp[c[v]][v][1][0][0] = 1;

	for(auto u:G[v])
	{
		dfs(u);

		for(int degv = 2; degv >= 0; --degv)
		{
			for(int sv = sub[v]; sv >= 1; --sv)
			{
				for(int pv = 0; pv < 2; ++pv)
				{
					for(int degu = 2; degu >= 0; --degu)
					{
						for(int su = sub[u]; su >= 1; --su)
						{
							for(int pu = 0; pu < 2; ++pu)
							{
								if(degv<=1 && degu<=1) //uzywam krawedzi
								{
									int ndeg = degv+1;
									int np = (pv+pu+1)%2;
									int ns = sv+su-1;
									int mult = 1;

									if(degv==0 && degu==0)
										mult *= 2;
									else if(degv==1 && degu==1)
										mult *= inv2;

									dp[c[v]^1][v][ns][ndeg][np] += mult*dp[c[v]][v][sv][degv][pv]%mod*dp[c[u]][u][su][degu][pu]%mod;
									dp[c[v]^1][v][ns][ndeg][np] %= mod;
								}

								//nie uzywam krawedzi

								int ndeg = degv;
								int np = (pv+pu)%2;
								int ns = sv+su;

								dp[c[v]^1][v][ns][ndeg][np] += dp[c[v]][v][sv][degv][pv]*dp[c[u]][u][su][degu][pu]%mod;
								dp[c[v]^1][v][ns][ndeg][np] %= mod;
							}
						}
					}

					dp[c[v]][v][sv][degv][pv] = 0;
				}	
			}
		}

		c[v] ^= 1;
		sub[v] += sub[u];
	}
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	cin >> n;

	for(int i = 2; i <= n; ++i)
	{
		int x;
		cin >> x;
		G[x].pb(i);
	}

	dfs(1);

	int fact = 1;
	int ans = 0;

	for(int i = 1; i <= n; ++i)
	{
		fact = fact*i%mod;

		for(int deg = 0; deg < 3; ++deg)
		{
			ans -= fact*dp[c[1]][1][i][deg][1]%mod;
			ans += fact*dp[c[1]][1][i][deg][0]%mod;
			ans %= mod;
		}
	}

	ans += mod;
	ans %= mod;

	cout << ans << endl;
}