#include<bits/stdc++.h>
using namespace std;
#define MAX_V 2050
#define int long long
int MOD=1000000007;
int V;
vector<int> G[MAX_V],rG[MAX_V],nG[MAX_V],vs,kv[MAX_V];
bool used[MAX_V];
int cmp[MAX_V];
void add_edge(int from,int to){
  G[from].push_back(to);
  rG[to].push_back(from);
}
void dfs(int v){
  used[v]=1;
  for(int i=0;i<(int)G[v].size();i++){
    if(!used[G[v][i]]) dfs(G[v][i]);
  }
  vs.push_back(v);
}
void rdfs(int v,int k){
  used[v]=1;
  cmp[v]=k;
  kv[k].push_back(v);
  for(int i=0;i<(int)rG[v].size();i++){
    if(!used[rG[v][i]]) rdfs(rG[v][i],k);
  }
}
int dfs2(int v,int f){
  int res=0,tmp=1;
  if(!f){
    for(int i=0;i<(int)nG[v].size();i++) tmp=(tmp*dfs2(nG[v][i],0))%MOD;
    res=tmp;tmp=1;
  }
  for(int i=0;i<(int)nG[v].size();i++) tmp=(tmp*dfs2(nG[v][i],1))%MOD;
  //cout<<v<<":"<<f<<"/"<<res+tmp<<endl;
  return (res+tmp)%MOD;
}
int scc(){
  memset(used,0,sizeof(used));
  vs.clear();
  for(int v=0;v<V;v++){
    if(!used[v]) dfs(v);
  }
  memset(used,0,sizeof(used));
  int k=0;
  for(int i=vs.size()-1;i>=0;i--){
    if(!used[vs[i]]) rdfs(vs[i],k++);
  }
  memset(used,0,sizeof(used));
  int ans=1;
  for(int i=0;i<k;i++){
    for(int j=0;j<(int)kv[i].size();j++){
      for(int l=0;l<(int)rG[kv[i][j]].size();l++){
	if(i!=cmp[rG[kv[i][j]][l]]){
	  nG[i].push_back(cmp[rG[kv[i][j]][l]]);
	  used[cmp[rG[kv[i][j]][l]]]=1;
	}
      }
    }
  }
  for(int i=0;i<V;i++){
    if(used[cmp[i]]) continue;
    //cout<<i<<":"<<ans<<endl;
    used[cmp[i]]=1;
    ans=(ans*dfs2(cmp[i],0))%MOD;
  }
  return ans;
}
signed main(){
  int n,m;
  cin>>n>>m;
  V=n;
  for(int i=0;i<m;i++){
    int s,d;
    cin>>s>>d;
    add_edge(s-1,d-1);
  }
  cout<<scc()<<endl;
  return 0;
}