#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <array>
#include <climits>
#include <bitset>
#include <cassert>
#include <random>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE 

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 310;
int dist[N][N];

struct SCC{
	int n, k;
	vector<vector<int>> G, RevG;
	vector<int> vs, used, cmp;

	SCC(int _n) :n(_n){
		G = RevG = vector<vector<int>>(n);
		used = cmp = vector<int>(n);
		k = -1;
	}

	void add_edge(int s, int t){
		G[s].push_back(t);
		RevG[t].push_back(s);
	}

	void dfs(int v){
		used[v] = 1;
		for (auto to : G[v]){
			if (!used[to]) dfs(to);
		}
		vs.push_back(v);
	}

	void rdfs(int v, int k){
		used[v] = 1;
		cmp[v] = k;
		for (auto to : RevG[v]){
			if (!used[to]) rdfs(to, k);
		}
	}

	int calcSCC(){
		fill(used.begin(), used.end(), 0);
		vs.clear();
		for (int i = 0; i < n; ++i) if(!used[i]) dfs(i);
		fill(used.begin(), used.end(), 0);
		k = 0;
		for (int i = n - 1; i >= 0; --i) if(!used[vs[i]]) rdfs(vs[i], k++);

		return k;
	}
};

vector<vector<int>> make_DAG(SCC &scc){
	if (scc.k < 0) scc.calcSCC();
	const auto &G = scc.G;
	const auto &cmp = scc.cmp;
	const int k = scc.k;

	vector<vector<int>> DAG(k);
	int n = G.size();
	for (int i = 0; i < n; ++i){
		for (auto to : G[i]){
			if(cmp[i] != cmp[to]) DAG[cmp[i]].push_back(cmp[to]);
		}
	}

	for (auto &v : DAG){
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end()), v.end());
	}
	return DAG;
}

vector<vector<int>> G;

const ll mod = 1e9 + 7;
int used[1010];

ll dfs(int v){
	used[v] = 1;
	ll res = 1;
	for (auto to : G[v]){
		res *= dfs(to);
		res %= mod;
	}
	return res + 1;
}


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;
	SCC scc(n);
	rep(i, m){
		int a, b;
		cin >> a >> b;
		scc.add_edge(b - 1, a - 1);
	}

	scc.calcSCC();
	G = make_DAG(scc);

	ll ans = 1;
	for (int i = 0; i < G.size(); ++i){
		if (used[i]) continue;
		ans *= dfs(i);
		ans %= mod;
	}
	cout << ans << endl;

	return 0;
}