eolymp
bolt
Try our new interface for solving problems
Problems

Alien`s Counting

Alien`s Counting

Natsuki and her friends were taken to the space by an alien and made friends with a lot of aliens. During the space travel, she discovered that aliens’ hands were often very different from humans’. Generally speaking, in a kind of aliens, there are \textbf{N} fingers and \textbf{M }bend rules on a hand. Each bend rule describes that a finger \textbf{A} always bends when a finger \textbf{B} bends. However, this rule does not always imply that the finger \textbf{B} bends when the finger \textbf{A} bends. When she were counting numbers with the fingers, she was anxious how many numbers her alien friends can count with the fingers. However, because some friends had too complicated rule sets, she could not calculate those. Would you write a program for her? \InputFile \textbf{N M S 1 D_1 S 2 D_2 ... S M D_M} The first line contains two integers \textbf{N} and \textbf{M} (\textbf{1} ≤ \textbf{N} ≤ \textbf{1000}; \textbf{0} ≤ \textbf{M} ≤ \textbf{1000}) in this order. The following \textbf{M} lines mean bend rules. Each line contains two integers \textbf{S}_i and \textbf{Di} in this order, which mean that the finger \textbf{Di} always bends when the finger \textbf{S_i} bends. Any finger appears at most once in \textbf{S}. \OutputFile Calculate how many numbers her alien friends can count with the fingers. Print the answer modulo \textbf{1000000007} in a line.
Time limit 8 seconds
Memory limit 32 MiB
Input example #1
5 4
2 3
3 4
4 3
5 4
Output example #1
10
Source ACM-ICPC Japan Alumni Group Summer Camp 2010, Day 4, Tokyo, Japan, 2010-09-20