#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

class StronglyConnectedComponent {
public:
  const ll N;
  vector<vector<ll>> E, R;
  vector<ll> component;

  StronglyConnectedComponent(ll N): N(N), E(N), R(N), component(N) {}

  void add_edge(ll u, ll v) {
    E[u].push_back(v);
    R[v].push_back(u);
  }

  ll discomposit() {
    vector<ll> vs;
    vector<bool> used1(N, false);
    REP(i, 0, N) if(!used1[i]) dfs(i, vs, used1);

    vector<bool> used2(N, false);
    ll k = 0;
    for(ll i = vs.size() - 1; i >= 0; i--) if(!used2[vs[i]]) rdfs(vs[i], k++, used2);
    return k;
  }

private:
  void dfs(ll v, vector<ll> &vs, vector<bool> &used) {
    used[v] = true;
    REP(i, 0, E[v].size()) if(!used[E[v][i]]) dfs(E[v][i], vs, used);
    vs.push_back(v);
  }

  void rdfs(ll v, ll k, vector<bool> &used) {
    used[v] = true;
    component[v] = k;
    REP(i, 0, R[v].size()) if(!used[R[v][i]]) rdfs(R[v][i], k, used);
  }
};

const ll MOD = 1000000007;

int main(void) {
  ll N, M;
  cin >> N >> M;
  vector<ll> S(M), D(M);
  StronglyConnectedComponent scc(N);
  REP(i, 0, M) {
    cin >> S[i] >> D[i]; S[i]--; D[i]--;
    scc.add_edge(S[i], D[i]);
  }

  ll n = scc.discomposit();
  vector<pll> edge;
  REP(i, 0, M) edge.push_back(pll(scc.component[S[i]], scc.component[D[i]]));
  sort(edge.begin(), edge.end());
  edge.erase(unique(edge.begin(), edge.end()), edge.end());

  vector<vector<ll>> E(n), R(n);
  REP(i, 0, edge.size()) {
    ll s = edge[i].first, d = edge[i].second;
    if(s == d) continue;
    E[s].push_back(d);
    R[d].push_back(s);
  }

  vector<ll> cnt(n);
  REP(i, 0, n) cnt[i] = R[i].size();

  stack<ll> stk;
  REP(i, 0, n) if(cnt[i] == 0) stk.push(i);

  vector<ll> dp(n, 0);
  while(stk.size()) {
    ll v = stk.top();
    stk.pop();

    dp[v] = 1;
    REP(i, 0, R[v].size()) (dp[v] *= dp[R[v][i]]) %= MOD;
    (dp[v] += 1) %= MOD;

    REP(i, 0, E[v].size()) if(--cnt[E[v][i]] == 0) stk.push(E[v][i]);
  }

  ll ans = 1;
  REP(i, 0, n) if(E[i].size() == 0) (ans *= dp[i]) %= MOD;
  cout << ans << endl;
}
