#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

void stronglyConnectedComponents(const vector<vector<int> >& edges1, vector<vector<int> >& nodes, vector<vector<int> >& edges2)
{
    const int n = edges1.size();

    class Func{
        vector<int> num, low;
        vector<bool> isStk;
        stack<int> stk;
        int time;
    public:
        Func(int n){
            num.assign(n, 0);
            low.assign(n, 0);
            isStk.assign(n, false);
            time = 0;
        }
        void visit(const vector<vector<int> >& edges, int v, vector<vector<int> >& nodes){
            if(num[v] != 0)
                return;
            num[v] = low[v] = ++ time;
            stk.push(v);
            isStk[v] = true;
            for(unsigned i=0; i<edges[v].size(); ++i){
                int w = edges[v][i];
                visit(edges, w, nodes);
                if(isStk[w])
                    low[v] = min(low[v], low[w]);
            }
            if(low[v] == num[v]){
                nodes.push_back(vector<int>());
                for(;;){
                    int w = stk.top();
                    stk.pop();
                    isStk[w] = false;
                    nodes.back().push_back(w);
                    if(v == w)
                        break;
                }
            }
        }
    }func(n);

    nodes.clear();
    for(int i=0; i<n; ++i)
        func.visit(edges1, i, nodes);
    reverse(nodes.begin(), nodes.end()); // DAGにするために反転させる

    const int m = nodes.size();
    vector<int> index(n); // 元のグラフの各頂点のDAGにおける頂点
    for(int i=0; i<m; ++i){
        for(unsigned j=0; j<nodes[i].size(); ++j)
            index[nodes[i][j]] = i;
    }
    edges2.assign(m, vector<int>());
    vector<vector<bool> > check(m, vector<bool>(m, false));
    for(int i=0; i<n; ++i){
        for(unsigned j=0; j<edges1[i].size(); ++j){
            int v = index[i];
            int w = index[edges1[i][j]];
            if(v != w && !check[v][w]){
                edges2[v].push_back(w);
                check[v][w] = true;
            }
        }
    }
}

const int MOD = 1000000007;

long long solve(const vector<vector<int> >& edges, int curr)
{
    long long ret = 1;
    for(unsigned i=0; i<edges[curr].size(); ++i){
        ret *= solve(edges, edges[curr][i]);
        ret %= MOD;
    }
    ++ ret;
    ret %= MOD;

    return ret;
}

int main()
{
    int n, m;
    cin >> n >> m;

    vector<vector<int> > edges0(n);
    for(int i=0; i<m; ++i){
        int s, d;
        cin >> s >> d;
        edges0[d-1].push_back(s-1);
    }

    vector<vector<int> > edges, nodes;
    stronglyConnectedComponents(edges0, nodes, edges);
    n = edges.size();

    vector<bool> check(n, false);
    for(int i=0; i<n; ++i){
        for(unsigned j=0; j<edges[i].size(); ++j){
            check[edges[i][j]] = true;
        }
    }

    long long ret = 1;
    for(int i=0; i<n; ++i){
        if(!check[i]){
            ret *= solve(edges, i);
            ret %= MOD;
        }
    }
    cout << ret << endl;

    return 0;
}

Measured memory_bytes: 1532