You are playing an MMORPG where abilities are unlocked by points which you earn upon leveling up. Of course, abilities can only be learned in certain orders. For example, the one-handed sword skill must be first learned before learning the dual-wielding skill, and Vorpal Strike is a prerequisite for Starburst Stream. Abilities that have a prerequisite ability are known as advanced abilities, whereas abilities that have no requirements to unlock are called basic abilities. In this particular game, all advanced abilities have exactly one prerequisite ability. Naturally, it is guaranteed that the prerequisites are never cyclical — that is, there will never be a case where ability
There are a total of
Constraints
Subtask 1 [10%]
Subtask 2 [20%]
Subtask 3 [20%]
Subtask 4 [50%]
Input Specification
The first line of input will contain
The next line will contain
Output Specification
The number of different orders in which you can unlock all the abilities, modulo
Sample Input
4
0 1 1 0
Sample Output
8
Explanation for Sample Output
There are a total of 4 abilities, where
Comments