#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 v; rep(j,i,n)if(mat[j][i]!=0)v.push_back(j); if(v.empty())return 0; int idx=v[mt()%v.size()]; if(idx!=i){ rep(j,0,n)swap(mat[i][j],mat[idx][j]); res*=-1; } res*=mat[i][i]; Mint inv=mat[i][i].inv(); rep(j,0,n)mat[i][j]*=inv; rep(j,i+1,n)if(mat[j][i]!=0){ inv=mat[j][i]; rep(k,i,n)if(mat[i][j]!=0)mat[j][k]-=mat[i][k]*inv; } } return res; } factorial fact(8020); 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)mat[i][j]=g[v[i]][v[j]]; res*=det(N-1); printf("%d\n",res.val); return 0; }