Ranking
Time limit2sMemory limit512 MB
Given N-1 comparisons where tower a is taller than b, and each tower loses to a taller tower at most once, count the possible total order tables consistent with these facts.
- Level
Medium7 of 10
- Topics
- Combinatorics, Tree, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
In the town where Ikuta lives there are towers. Each tower is given a distinct number from 0 to , and the tower numbered is called tower . Curious Ikuta took an interest in the heights of the towers and decided to build a table that describes their order. has elements, and each element is defined as follows.
-
the height of tower is less than the height of tower
-
the height of tower equals the height of tower
-
the height of tower is greater than the height of tower
To build the table , Ikuta repeatedly chose two towers and compared their heights, times in total.
The following is known about Ikuta's survey.
-
If tower and tower were chosen in the -th comparison , then the height of tower was greater than the height of tower . That is, and .
-
Each tower was compared with a taller tower at most once.
Unfortunately, the information from Ikuta's survey alone does not always determine the contents of table uniquely. When does not contradict Ikuta's survey and there exists a combination of tower heights for which is defined, call a correct table. Compute how many correct tables are possible and tell Ikuta.
Note that the two compared towers have different heights, but not all tower heights are necessarily different.
Input
The input is given in the following format.
...
represents the number of towers. , () mean that tower is taller than tower .
Output
Output the number of possible correct tables modulo 1,000,000,007.
Constraints
Each variable in the input satisfies the following conditions.
-
-
-
-
Different towers may have the same height.
-
Ikuta's survey results themselves are consistent, and at least one table exists that does not contradict the survey results.