Problem:
Problem A: 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 N fingers and M bend rules on a hand. Each bend rule describes that a finger A always bends when a finger B bends. However, this rule does not always imply that the finger B bends when the finger 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?

Input

N M

S1 D1

S2 D2

.

.

.

SM DM

The first line contains two integers N and M (1 ≤ N ≤ 1000, 0 ≤ M ≤ 1000) in this order. The following M lines mean bend rules. Each line contains two integers Si and Di in this order, which mean that the finger Di always bends when the finger Si bends. Any finger appears at most once in S.

Output

Calculate how many numbers her alien friends can count with the fingers. Print the answer modulo 1000000007 in a line.

Sample Input 1

5 4
2 3
3 4
4 3
5 4

Output for the Sample Input 1

10

Sample Input 2

5 5
1 2
2 3
3 4
4 5
5 1

Output for the Sample Input 2

2

Sample Input 3

5 0

Output for the Sample Input 3

32

Solution in Ruby (judge verdict: Accepted)

require 'tsort'
require 'set'

n, m = gets.split.map(&:to_i)

class Hash
    include TSort
    alias :tsort_each_node :each_key
    def tsort_each_child(i)
        self[i].each{|j| yield j}
    end
end

orig_graph = {}
(1..n).each { |i| orig_graph[i] = Set.new }
edges = (1..m).map { gets.split.map(&:to_i) }
edges.each { |i, j| orig_graph[j] << i }

gc = {}
dec = orig_graph.strongly_connected_components
dec.each_with_index do |a, i|
    a.each { |j| gc[j] = i }
end

graph = {}
(0..dec.size-1).each {|i| graph[i] = Set.new }
edges.each do |i, j|
    graph[gc[j]] << gc[i] unless gc[i] == gc[j]
end

roots = Set[*graph.keys]
graph.each { |_, children| roots -= children }

def f(graph, i)
    s = 1 + graph[i].map{|j| f(graph, j)}.inject(1, :*)
    s % 1_000_000_007
end

p roots.map{|i| f(graph, i)}.inject(1, :*) % 1_000_000_007