#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; templateinline bool chmax(T& a,T b){if(ainline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} //end templatestruct mint { unsigned val; static unsigned get_mod(){return mod;} unsigned inv() const{ int tmp,a=val,b=mod,x=1,y=0; while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y); if(x<0)x+=mod; return x; } mint():val(0){} mint(ll x):val(x>=0?x%mod:mod+(x%mod)){} mint pow(ll t){mint res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;} mint& operator+=(const mint& x){if((val+=x.val)>=mod)val-=mod;return *this;} mint& operator-=(const mint& x){if((val+=mod-x.val)>=mod)val-=mod; return *this;} mint& operator*=(const mint& x){val=ll(val)*x.val%mod; return *this;} mint& operator/=(const mint& x){val=ll(val)*x.inv()%mod; return *this;} mint operator+(const mint& x)const{return mint(*this)+=x;} mint operator-(const mint& x)const{return mint(*this)-=x;} mint operator*(const mint& x)const{return mint(*this)*=x;} mint operator/(const mint& x)const{return mint(*this)/=x;} bool operator==(const mint& x)const{return val==x.val;} bool operator!=(const mint& x)const{return val!=x.val;} }; using Mint=mint<>; templatestruct factorial { vector Fact,Finv,Inv; factorial(int maxx){ Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx); Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1; unsigned mod=Mint::get_mod(); rep(i,2,maxx){ Fact[i]=Fact[i-1]*i; Inv[i]=Inv[mod%i]*(mod-mod/i); Finv[i]=Finv[i-1]*Inv[i]; } } T fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];} T inv(int n){return Inv[n];} T nPr(int n,int r){if(n<0||n BM(vector a){ int n=a.size(); vector b({1}),c({1}); Mint d=1; rep(j,1,n+1){ int l=c.size(),m=b.size(); Mint x=0; rep(i,0,l)x+=c[i]*a[j-l+i]; b.push_back(0); m++; if(x==0)continue; Mint coeff=x/d*-1; if(l> T; factorial fact(8020); mt19937 mt(1341398); vector mat; vector make(int n){ vector res(n); rep(i,0,n)res[i]=mt()%(Mint::get_mod()-1)+1; return res; } Mint det(int n){ if(n<=0)return 1; while(1){ vector v=make(n),m=make(n),r=make(n); vector sub=mat; rep(i,0,mat.size()){ int x=mat[i].second.first,y=mat[i].second.second; sub[i].first*=v[y]; } vector w(n*2); rep(i,0,n*2){ rep(j,0,n)w[i]+=m[j]*r[j]; vector nxt(n); Mint c; rep(j,0,n)c-=m[j]*v[j]; rep(j,0,n)nxt[j]=c; rep(j,0,sub.size()){ int x=sub[j].second.first,y=sub[j].second.second; nxt[x]+=m[y]*(sub[j].first+v[y]); } swap(m,nxt); } auto ret=BM(w); if(ret[0]==0)return 0; if(ret.size()!=n+1)continue; Mint res=ret[0]; if(n&1)res*=-1; rep(i,0,n)res/=v[i]; return res; } } int g[4010][4010]={},in[4010],out[4010]; void fail(){puts("0"); exit(0);} int main(){ int n,m; scanf("%d%d",&n,&m); if(n*n==m){puts("1"); return 0;} rep(i,0,n){ g[i][i]=in[i]=out[i]=n; rep(j,0,n)g[i][j]--; } rep(_,0,m){ int x,y; scanf("%d%d",&x,&y); x--; y--; in[y]--; out[x]--; g[x][y]++; g[y][y]--; } int s=-1,t=-1,N=0,v[4010]; rep(i,0,n){ if(in[i]==0 and out[i]==0)continue; v[N++]=i; if(abs(in[i]-out[i])>1)fail(); if(out[i]==in[i]+1){ if(s!=-1)fail(); s=i; } if(out[i]+1==in[i]){ if(t!=-1)fail(); t=i; } } Mint res=n*n-m; if(s!=-1 and t!=-1){ in[s]++; out[t]++; g[s][t]--; g[s][s]++; res=1; } rep(i,0,N)res*=fact.fact(in[v[i]]-1); rep(i,0,N-1)rep(j,0,N-1)if(g[v[i]][v[j]]!=-1){ mat.push_back({Mint(g[v[i]][v[j]]),{i,j}}); } res*=det(N-1); printf("%d\n",res.val); return 0; }