#include <bits/stdc++.h>
#define mod 1000000007
#define MAX_V 100000
#define N MAX_V
using namespace std;
typedef long long ll;
int V;
vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> vs;
bool used[MAX_V];
int cmp[MAX_V];
int n,m;

void add_edge(int from,int to){
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v){
  used[v]=true;
  for(int i=0;i<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]=true;
  cmp[v]=k;
  for(int i=0;i<rG[v].size();i++) if(!used[rG[v][i]])rdfs(rG[v][i],k);
}

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++);
  return k;
}


void mk_G(){
  int k=scc();
  vector <int> tmp[N],rtmp[N];
  for(int i=0;i<n;i++){
    for(int j=0;j<G[i].size();j++)
      if(cmp[i]!=cmp[G[i][j]]) tmp[cmp[i]].push_back(cmp[G[i][j]]);
    for(int j=0;j<rG[i].size();j++)
      if(cmp[i]!=cmp[rG[i][j]]) rtmp[cmp[i]].push_back(cmp[rG[i][j]]);
    G[i].clear();
    rG[i].clear();
  }
  n=k;
  for(int i=0;i<k;i++) G[i]=tmp[i],rG[i]=rtmp[i];
}
 
ll calc(int pos){
  if(used[pos]) return 1;
  used[pos]=1;
  ll res=1;
  for(int i=0;i<rG[pos].size();i++)res=res*calc(rG[pos][i])%mod;
  return (res+1)%mod;
}
 

int main(){
  cin>>n>>m;
  V=n;
  for(int i=0,a,b;i<m;i++)cin>>a>>b,add_edge(a-1,b-1);
  mk_G();
  ll ans=1;
  memset(used,0,sizeof(used));
  for(int i=0;i<n;i++) if(!G[i].size())ans=ans*calc(i)%mod;
  cout <<ans<<endl;
  return 0;
}