#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

#include<stack>
vector<vi> g;
void visit(int v, vector< vector<int> >& scc,
    stack<int> &S, vector<bool> &inS,
    vector<int> &low, vector<int> &num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v); inS[v] = true;
  each(e, g[v]) {
    int w = *e;
    if (num[w] == 0) {
      visit(w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top(); S.pop(); inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}
void stronglyConnectedComponents(vector< vector<int> >& scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  rep(u, n) if (num[u] == 0)
    visit(u, scc, S, inS, low, num, time);
}

const int mod = (int)1e9 + 7;
int n, m, to[2000], deg[2000];
ll rec(const vector<vi> &e, int c){
	ll res = 1;
	each(i, e[c]) (res *= rec(e, *i)) %= mod;
	return res + 1;
}

int main(){
	cin >> n >> m;
	g.resize(n);
	rep(i, m){
		int a, b;
		cin >> a >> b;
		g[b - 1].pb(a - 1);
	}
	vector<vi> scc;
	stronglyConnectedComponents(scc);
	set<pi> es;
	int N = scc.size();
	vector<vi> e(N + 1);
	
	rep(i, N) each(j, scc[i]) to[*j] = i;
	rep(i, n) each(j, g[i]){
		int a = to[i], b = to[*j];
		if(es.count(mp(a, b)) || a == b) continue;
		es.insert(mp(a, b));
		e[a].pb(b);
		deg[b]++;
	}
	rep(i, N) if(deg[i] == 0) e[N].pb(i);
	cout << (rec(e, N) + mod - 1) % mod << endl;
	return 0;
}