atcoder#CF17EXHIBITIONA. Awkward
Awkward
Score : 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 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 to , and the member with the ID is called Member . The president is Member , and the direct boss of Member is Member .
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 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 .
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print the number of ways for the members to stand in a line, such that no member except the president is next to his/her direct boss, modulo .
4
1
2
3
2
Below, we will write to denote the fact that Member is the direct boss of Member .
In this case, the hierarchy of the members is . There are two ways for them to stand in a line that satisfy their desires:
Note that the latter is the reverse of the former, but we count them individually.
3
1
2
0
The hierarchy of the members is . No matter what order they stand in, at least one of the desires of Member and is not satisfied, so the answer is .
5
1
1
3
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:
15
1
2
3
1
4
2
7
1
8
2
8
1
8
2
97193524