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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

public class Main {
    private FastScanner in;
    private PrintWriter out;

    final int mod = (int) 1e9 + 7;

    ArrayList<Integer>[] g;


    long[][] go(int v) {
        long[][] dp = new long[3][1];
        dp[0][0] = 1;
        for (int to : g[v]) {
            long[][] child = go(to);
            int maxK = child[0].length;
            long[][] ndp = new long[3][dp[0].length + maxK];
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < dp[i].length; j++) {
                    long cur = dp[i][j];
                    if (cur == 0) {
                        continue;
                    }
                    for (int i2 = 0; i2 < 2; i2++) {
                        for (int j2 = 0; j2 < child[i2].length; j2++) {
                            long chWays = child[i2][j2];
                            if (chWays == 0) {
                                continue;
                            }
                            int nextI = i + i2;
                            if (nextI >= 3) {
                                continue;
                            }
                            int nextJ = j + j2;
                            ndp[nextI][nextJ] = (int) ((ndp[nextI][nextJ] + cur * chWays) % mod);
                        }
                    }
                }
            }
            dp = ndp;
        }
        long[][] res = new long[2][dp[0].length + 1];
        for (int j = 0; j < dp[0].length; j++) {
            {
                long cur0 = dp[0][j];
                res[0][j] += cur0;
                if (res[0][j] >= mod) {
                    res[0][j] -= mod;
                }
                res[1][j + 1] += cur0;
                if (res[1][j + 1] >= mod) {
                    res[1][j + 1] -= mod;
                }
            }
            {
                res[0][j] += dp[1][j] + dp[1][j];
                while (res[0][j] >= mod) {
                    res[0][j] -= mod;
                }
                res[1][j + 1] += dp[1][j];
                if (res[1][j + 1] >= mod) {
                    res[1][j + 1] -= mod;
                }
            }
            {
                res[0][j] += dp[2][j] + dp[2][j];
                while (res[0][j] >= mod) {
                    res[0][j] -= mod;
                }
            }
        }
        return res;
    }

    private void solve() {
        int n = in.nextInt();
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 1; i < n; i++) {
            g[in.nextInt() - 1].add(i);
        }
        long[][] go = go(0);
        long[] fact = new long[n + 1];
        fact[0] = 1;
        for (int i = 1; i < fact.length; i++) {
            fact[i] = fact[i - 1] * i % mod;
        }
        long result = 0;
        for (int i = 0; i < go[0].length; i++) {
            if (go[0][i] == 0) {
                continue;
            }
            long ways = go[0][i] * fact[n - i] % mod;
            if (i % 2 == 1) {
                ways = mod - ways;
            }
            result += ways;
        }
        out.println(result % mod);
    }

    private void run() {
        try {
            in = new FastScanner(new File("CF17_Exhibition_A.in"));
            out = new PrintWriter(new File("CF17_Exhibition_A.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void runIO() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    private class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}