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



Sample input:
4
1
2
3


Sample output:
2

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

numAns = 0

class Node:
    def __init__(self,init):
        self.addr = None
        self.boss = init
        self.subordinate = []
    def printBoss(self):
        print(self.boss)
    def showTree(self,tmp_node):
        print(self.addr,self.boss,self.subordinate)
        for sub in self.subordinate:
            # print("-",end="")
            tmp_node[sub-1].showTree(tmp_node)
    def findTree(self,N,relation,tmp_node):
        relation.append(self.addr)

        if N == len(relation):
            global numAns
            numAns = (numAns+1) % 1000000007
            
            relation.pop()
            return

        list = [True for i in range(N)]
        list[int(self.addr)-1] = False
        if self.boss is not None:
            list[int(self.boss)-1] = False
        for sub in self.subordinate:
            list[int(sub)-1] = False
        for rel in relation:
            list[int(rel)-1] = False
        
        for i in range(len(list)):
            if list[i] is True:
                tmp_node[i].findTree(N,relation,tmp_node)
        relation.pop()
                
        
def input_boss(boss):
    N = input()
    for i in range(int(N)-1):
        boss.append(input())
    return N

def make_tree(nodes,boss):
    for num in boss:
        nodes.append(Node(num))

    nodes.insert(0,Node(None))
    nodes[0].addr = 1

    for i in range(1,len(nodes)):
        nodes[i].addr = i+1
        tmp_boss = int(nodes[i].boss)-1
        nodes[tmp_boss].subordinate.append(i+1)
    
    # for node in nodes:
    #     print(node.addr,node.boss,node.subordinate)

if __name__ == '__main__':

    boss =[]
    
    N = input_boss(boss)

    nodes = []
    make_tree(nodes,boss)

    # nodes[0].showTree(nodes)
    for i in range(len(nodes)):
        relation = []
        nodes[i].findTree(int(N),relation,nodes)
    
    print(numAns)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

numAns = 0

class Node:
    def __init__(self,init):
        self.addr = None
        self.boss = init
        self.subordinate = []
    def printBoss(self):
        print(self.boss)
    def showTree(self,tmp_node):
        print(self.addr,self.boss,self.subordinate)
        for sub in self.subordinate:
            # print("-",end="")
            tmp_node[sub-1].showTree(tmp_node)
    def findTree(self,N,relation,tmp_node):
        relation.append(self.addr)

        if N == len(relation):
            global numAns
            numAns += 1
            relation.pop()
            return

        list = [True for i in range(N)]
        list[int(self.addr)-1] = False
        if self.boss is not None:
            list[int(self.boss)-1] = False
        for sub in self.subordinate:
            list[int(sub)-1] = False
        for rel in relation:
            list[int(rel)-1] = False
        
        for i in range(len(list)):
            if list[i] is True:
                tmp_node[i].findTree(N,relation,tmp_node)
        relation.pop()
                
        
def input_boss(boss):
    N = input()
    for i in range(int(N)-1):
        boss.append(input())
    return N

def make_tree(nodes,boss):
    for num in boss:
        nodes.append(Node(num))

    nodes.insert(0,Node(None))
    nodes[0].addr = 1

    for i in range(1,len(nodes)):
        nodes[i].addr = i+1
        tmp_boss = int(nodes[i].boss)-1
        nodes[tmp_boss].subordinate.append(i+1)
    
    # for node in nodes:
    #     print(node.addr,node.boss,node.subordinate)

if __name__ == '__main__':

    boss =[]
    
    N = input_boss(boss)

    nodes = []
    make_tree(nodes,boss)

    # nodes[0].showTree(nodes)
    for i in range(len(nodes)):
        relation = []
        nodes[i].findTree(int(N),relation,nodes)
    
    print(numAns)