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