Gomgomi's Last-Minute Cramming
Time limit1sMemory limit1024 MB
Count the orders for watching N lectures under prerequisite relations that form a forest, modulo 10^9+7.
- Level
Medium6 of 10
- Topics
- Tree, Combinatorics, Dynamic programming
- Solved
- No attempts yet
Problem

With the final exam approaching, Gomgomi has backlogged lectures to watch. The lectures are numbered from to in order. Each lecture has at most one lecture that must be watched before it. Gomgomi writes this relation as , meaning lecture must be watched before lecture . The relations contain no cycles. The order in which Gomgomi watches all the lectures depends on these relations. Find the number of orders for watching all the lectures. Since the answer can be large, print it modulo .
Input
The first line contains the number of lectures and the number of relations .
Each of the next lines contains integers and , separated by a space. This means lecture must be watched before lecture . The relations contain no cycles, and each lecture has at most one lecture that must be watched before it.
Output
Print the number of orders for watching all the lectures, modulo .