#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

struct Edge { int from, to; };
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &g, int from, int to) {
	g[from].push_back(Edge{ from, to });
}

void dfs(const Graph& g, int v, vector<bool>& used, vi& vs)
{
	used[v] = true;
	for (auto i : g[v])
	{
		if (!used[i.to]) dfs(g, i.to, used, vs);
	}
	vs.push_back(v);
}

void rdfs(const Graph& rg, int v, vector<bool>& used, int k, vi& cmp)
{
	used[v] = true;
	cmp[v] = k;
	for (auto i : rg[v])
	{
		if (!used[i.to]) rdfs(rg, i.to, used, k, cmp);
	}
}

int strongly_connected_component(Graph graph, vi& cmp)
{
	int V = graph.size();
	Graph g(V), rg(V);
	vector<bool> used(V);
	vi vs;
	cmp.resize(V);
	REP(i, V)
	{
		for (auto e : graph[i])
		{
			add_edge(g, i, e.to);
			add_edge(rg, e.to, i);
		}
	}
	REP(i, V)
	{
		if (!used[i]) dfs(g, i, used, vs);
	}
	used = vector<bool>(V);
	reverse(ALL(vs));
	int k = 0;
	for (auto i : vs)
	{
		if (!used[i]) rdfs(rg, i, used, k++, cmp);
	}
	return k;
}

Graph buildGraph(const Graph& g, const vi& cmp, int K)
{
	int V = g.size();
	vector<set<int>> s(K);
	Graph res(K);
	REP(i, V)
	{
		for (auto e : g[i]) s[cmp[i]].insert(cmp[e.to]);
	}
	REP(i, K)
	{
		for (auto j : s[i]) if (i != j) add_edge(res, i, j);
	}
	return res;
}

ll solve(const vector<vi>& tree, int v)
{
	ll res = 1, tmp = 1;
	REP(i, tree[v].size())
	{
		tmp *= solve(tree, tree[v][i]);
		tmp %= MOD;
	}
	res += tmp;
	return res;
}

int main()
{
	int n, m;
	cin >> n >> m;
	Graph g(n);
	REP(i, m)
	{
		int a, b;
		cin >> a >> b;
		a--; b--;
		add_edge(g, a, b);
	}
	vi cmp;
	int k = strongly_connected_component(g, cmp);
	Graph dag = buildGraph(g, cmp, k);
	vector<vi> tree(n);
	vi root;
	REP(i, dag.size())
	{
		if (dag[i].size() == 0) root.push_back(i);
		for (auto e : dag[i])
		{
			tree[e.to].push_back(e.from);
		}
	}
	ll ans = 1;
	REP(i, root.size())
	{
		ans *= solve(tree, root[i]);
		ans %= MOD;
	}
	cout << ans << endl;
	return 0;
}