extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::fmt::{Display, Error, Formatter};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use std::collections::{BTreeMap, VecDeque, BinaryHeap};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

fn main() {
    const MOD: i64 = 1000000007;
    let_all!(n: usize, m: usize);
    let mut fingers = vec![0; n + 1];
    let mut constraint_count = vec![0; n + 1];
    constraint_count[0] = n + 1;
    for _ in 0 .. m {
        let_all!(s: usize, d: usize);
        fingers[s] = d;
        constraint_count[d] += 1;
    }
    let mut stack = VecDeque::new();
    let mut patterns = vec![1_i64; n + 1];
    for i in 1 .. constraint_count.len() {
        if constraint_count[i] == 0 {
            stack.push_back(i);
        }
    }
    while let Some(current) = stack.pop_back() {
        patterns[current] = (patterns[current] + 1) % MOD;
        patterns[fingers[current]] = (patterns[fingers[current]] * patterns[current]) % MOD;
        constraint_count[fingers[current]] -= 1;
        if constraint_count[fingers[current]] == 0 {
            stack.push_back(fingers[current]);
        }
    }
    for i in 1 .. constraint_count.len() {
        if constraint_count[i] == 1 {
            constraint_count[i] -= 1;
            stack.push_back(fingers[i]);
            while let Some(current) = stack.pop_back() {
                constraint_count[current] -= 1;
                patterns[fingers[current]] = (patterns[current] * patterns[fingers[current]]) % MOD;
                if constraint_count[fingers[current]] == 1 {
                    stack.push_back(fingers[current]);
                }
            }
            patterns[0] = (patterns[0] * (patterns[i] + 1)) % MOD;
        }
    }
    println!("{}", patterns[0]);
}


Measured memory_bytes: 5056