package q8xx; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Random; public class Q874_5 { InputStream is; PrintWriter out; String INPUT = ""; // String INPUT = "10 0"; // String INPUT = "4 7 1 2 2 1 2 3 3 2 2 4 4 2 2 2"; boolean[][] shrink(boolean[][] ng) { int n = ng.length; int[] outdeg = new int[n]; int[] indeg = new int[n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(!ng[i][j]){ outdeg[i]++; indeg[j]++; } } } int[] map = new int[n]; Arrays.fill(map, -1); int p = 0; for(int i = 0;i < n;i++){ if(!(outdeg[i] == 0 && indeg[i] == 0)){ map[i] = p++; } } boolean[][] nng = new boolean[p][p]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(ng[i][j] && map[i] != -1 && map[j] != -1){ nng[map[i]][map[j]] = true; } } } return nng; } long S,G; int mod = 1000000007; void solve() { int n = ni(), m = ni(); if(n*n == m){ out.println(1); return; } int on = n; boolean[][] ng = new boolean[n][n]; for(int i = 0;i < m;i++){ int f = ni()-1, t = ni()-1; ng[f][t] = true; } ng = shrink(ng); n = ng.length; int[] outdeg = new int[n]; int[] indeg = new int[n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(!ng[i][j]){ outdeg[i]++; indeg[j]++; } } } // connectivity DJSet ds = new DJSet(n); for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(!ng[i][j]){ ds.union(i, j); } } } for(int i = 0;i < n;i++){ for(int j = i+1;j < n;j++){ if( (outdeg[i] >= 1 || indeg[i] >= 1) && (outdeg[j] >= 1 || indeg[j] >= 1) ){ if(!ds.equiv(i, j)){ out.println(0); return; } } } } int src = -1, sink = -1; for(int i = 0;i < n;i++){ if(outdeg[i] == indeg[i])continue; if(outdeg[i] == indeg[i]+1 && src == -1){ src = i; continue; } if(outdeg[i] == indeg[i]-1 && sink == -1){ sink = i; continue; } out.println(0); return; } int[][] MM = new int[n+1][n+1]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(i == j){ MM[i+1][i+1]++; }else if(!ng[i][j]){ MM[i+1][j+1] = 0; MM[i+1][i+1]++; }else{ MM[i+1][j+1] = 1; } } } MM[0][0] = 1; for(int i = 1;i < n+1;i++)MM[0][i] = mod-1; for(int i = 1;i < n+1;i++)MM[i][0] = mod-1; if(src == -1){ out.println(numEulerianCircuit(MM, outdeg, indeg) * (on*on-m) % mod); }else{ // int u = MM[sink+1][src+1] == 0 ? 2 : 1; if(--MM[sink+1][src+1] < 0)MM[sink+1][src+1] += mod; MM[sink+1][sink+1]++; outdeg[sink]++; indeg[src]++; long nec = numEulerianCircuit(MM, outdeg, indeg); out.println(nec); } } long numEulerianCircuit(int[][] M, int[] outdeg, int[] indeg) { int n = M.length; long[] F = new long[n+2]; F[0] = 1; for(int i = 1;i <= n;i++)F[i] = F[i-1] * i % mod; long ec = 1; for(int i = 0;i < outdeg.length;i++){ ec = ec * F[outdeg[i]-1] % mod; } long[][][] QQ = denseToSparse(M, n-1); Random gen = new Random(0); long det = det(QQ, mod, gen); return ec * det % mod; } public long det(long[][][] A, int mod, Random gen) { int n = A.length; long div = 1L; long[] diag = new long[n]; for(int i = 0;i < n;i++){ diag[i] = gen.nextInt(mod-1)+1; div = div * diag[i] % mod; } for(int i = 0;i < n;i++){ for(long[] e : A[i]){ e[1] = e[1] * diag[(int)e[0]] % mod; } } long[] u = new long[n]; for(int i = 0;i < n;i++)u[i] = gen.nextInt(mod); long[] v = new long[n]; for(int i = 0;i < n;i++)v[i] = gen.nextInt(mod); long[] f = minPolynomialSparse(A, v, u, mod); if(n % 2 == 1)f[0] = mod-f[0]; return f[0] * invl(div, mod) % mod; } public static long[][][] denseToSparse(int[][] A, int n) { long[][][] ret = new long[n][][]; for(int i = 0;i < n;i++){ int nz = 0; for(int j = 0;j < n;j++)if(A[i][j] != 0)nz++; ret[i] = new long[nz][]; nz = 0; for(int j = 0;j < n;j++){ if(A[i][j] != 0){ ret[i][nz++] = new long[]{j, A[i][j]}; } } } return ret; } // public static long[] minPolynomial(long[][] M, long[] v, long[] u, int mod) // { // int n = M.length; // long[] co = new long[2*n]; // long[] w = Arrays.copyOf(v, n); // long big = 8L*mod*mod; // for(int i = 0;i < 2*n;i++){ // co[i] = 0L; // for(int j = 0;j < n;j++){ // co[i] += w[j] * u[j]; // if(co[i] >= big)co[i] -= big; // } // co[i] %= mod; // w = mul(M, w, mod); // } // // return modifiedBerlekampMassey(co, mod); // } public static long[] minPolynomialSparse(long[][][] M, long[] v, long[] u, int mod) { int n = M.length; // U -= System.nanoTime(); long[] co = new long[2*n]; long[] w = Arrays.copyOf(v, n); long big = 8L*mod*mod; for(int i = 0;i < 2*n;i++){ co[i] = 0L; for(int j = 0;j < n;j++){ co[i] += w[j] * u[j]; if(co[i] >= big)co[i] -= big; } co[i] %= mod; w = mulSparse(M, w, mod); } // U += System.nanoTime(); // tr("U", U); // long S = System.nanoTime(); long[] res = modifiedBerlekampMassey(co, mod); // 0.53/1.18 // long G = System.nanoTime(); // tr(G-S); return res; } public static long[] minPolynomialSparse(long[][][] M, long[] v, long[] u, int mod, int step) { int n = M.length; long[] co = new long[step]; long[] w = Arrays.copyOf(v, n); long big = 8L*mod*mod; for(int i = 0;i < step;i++){ co[i] = 0L; for(int j = 0;j < n;j++){ co[i] += w[j] * u[j]; if(co[i] >= big)co[i] -= big; } co[i] %= mod; w = mulSparse(M, w, mod); } return modifiedBerlekampMassey(co, mod); } public static long pow(long[][] M, long[] v, long[] u, long e, long[] minp, int mod) { int n = M.length; int q = minp.length-1; if(q == 0)return 0L; long[] rminp = new long[q]; for(int i = 0;i < q;i++)rminp[i] = mod-minp[i]; // x^e mod minp long[] coo = lrx(rminp, e, mod); long[] w = Arrays.copyOf(v, n); long ret = 0; for(int i = 0;i < q;i++){ long lret = 0; for(int j = 0;j < n;j++){ lret += w[j] * u[j]; lret %= mod; } ret += lret * coo[i]; ret %= mod; w = mul(M, w, mod); } return ret; } static long[] mul(long[][] M, long[] v, int mod) { int n = v.length; long[] w = new long[n]; for(int i = 0;i < n;i++){ long sum = 0; for(int j = 0;j < n;j++){ sum += M[i][j] * v[j]; sum %= mod; } w[i] = sum; } return w; } static long[] mulSparse(long[][][] M, long[] v, int mod) { int n = v.length; long[] w = new long[n]; long big = 8L*mod*mod; for(int i = 0;i < n;i++){ long sum = 0; for(long[] e : M[i]){ sum += e[1] * v[(int)e[0]]; if(sum >= big)sum -= big; } w[i] = sum % mod; } return w; } static int ct = 0; public static long[] modifiedBerlekampMassey(long[] a, int mod) { assert a.length % 2 == 0; int n = a.length/2; int m = 2*n-1; long[] R0 = new long[2*n+1]; R0[2*n] = 1; long[] R1 = new long[2*n]; for(int i = 0;i < 2*n;i++)R1[i] = a[m-i]; R1 = strip(R1); long[] v0 = {0L}; long[] v1 = {1L}; // TODO メモリを使い回す while(n <= R1.length-1){ long[][] QR = divmod(R0, R1, mod); // 0.34s? long[] v = sub(v0, mul(QR[0], v1, mod), mod); v0 = v1; v1 = v; R0 = R1; R1 = QR[1]; } // normalize long z = invl(v1[v1.length-1], mod); for(int i = 0;i < v1.length;i++){ v1[i] = v1[i] * z % mod; } return strip(v1); } static long U = 0; public static long[] mul(long[] a, long[] b, int mod) { long[] c = new long[a.length+b.length-1]; long big = 8L*mod*mod; for(int i = 0;i < a.length;i++){ for(int j = 0;j < b.length;j++){ c[i+j] += a[i] * b[j]; if(c[i+j] >= big)c[i+j] -= big; } } for(int i = 0;i < c.length;i++)c[i] %= mod; return strip(c); } public static long[] sub(long[] a, long[] b, int mod) { long[] c = Arrays.copyOf(a, Math.max(a.length, b.length)); for(int i = 0;i < c.length;i++){ c[i] -= b[i]; if(c[i] < 0)c[i] += mod; } return strip(c); } public static long[] strip(long[] a) { int i; for(i = a.length-1;i > 0 && a[i] == 0;i--); if(i + 1 == a.length)return a; return Arrays.copyOf(a, i+1); } public static long[][] divmod(long[] A, long[] B, int mod) { assert B[B.length-1] != 0; long[] R = A; long[] Q = new long[Math.max(1, A.length-B.length+1)]; long ib = invl(B[B.length-1], mod); long big = 8L*mod*mod; for(int i = A.length-1, t = Q.length-1;i >= B.length-1;i--,t--){ R[i] %= mod; if(R[i] < 0)R[i] += mod; long m = R[i] * ib % mod; Q[t] = m; for(int j = 0, k = i-B.length+1+j;j < B.length-1;j++,k++){ R[k] -= B[j] * m; // R[k] %= mod; // if(R[k] < 0)R[k] += mod; if(R[k] < -big)R[k] += big; } // R[i] %= mod; R[i] = 0; // assert R[i] == 0; } for(int i = B.length-2;i >= 0;i--){ R[i] %= mod; if(R[i] < 0)R[i] += mod; } // tr("A", A); // tr("B", B); // tr("Q", Q); // tr("R", R); return new long[][]{Q, strip(R)}; } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } public int count() { int ct = 0; for (int u : upper) if (u < 0) ct++; return ct; } } public static long[] lrx(long[] co, long n, long mod) { long BIG = (Long.MAX_VALUE - 2L*mod*mod)/mod*mod; int m = co.length; if(m == 1){ long ret = 1; long mul = co[0]; for(;n > 0;n >>>= 1){ if((n&1)==1){ ret = ret * mul % mod; } mul = mul * mul % mod; } return new long[]{ret}; } long[][] m2 = new long[m-1][m]; for(int j = 0;j < m;j++){ m2[0][j] = co[j]; } for(int i = 1;i < m-1;i++){ for(int j = 0;j < m;j++){ long x = m2[i-1][m-1] * co[j]; if(j-1 >= 0)x += m2[i-1][j-1]; m2[i][j] = x % mod; } } long[] ret = new long[m]; ret[0] = 1; long[] temp = new long[2*m]; for(int l = 62;l >= 0;l--){ if(n<<~l<0){ for(int i = 0;i < m;i++)temp[i] = ret[m-1] * co[i]; for(int i = 1;i < m;i++)temp[i] += ret[i-1]; for(int i = 0;i < m;i++)ret[i] = temp[i] % mod; } if(l > 0){ Arrays.fill(temp, 0L); for(int i = 0;i < m;i++){ for(int j = 0;j < m;j++){ temp[i+j] += ret[i] * ret[j]; if(temp[i+j] >= BIG)temp[i+j] -= BIG; } } for(int i = 0;i < 2*m-1;i++)temp[i] %= mod; for(int i = 0;i < m;i++){ long s = temp[i]; for(int j = m;j < 2*m-1;j++){ s += temp[j] * m2[j-m][i]; if(s >= BIG)s -= BIG; } ret[i] = s % mod; } } } return ret; } public static long invl(long a, long mod) { long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } return p < 0 ? p + mod : p; } void run() throws Exception { // int n = 4000, m = 99999; // Random gen = new Random(0); // StringBuilder sb = new StringBuilder(); // sb.append(n+ " "); // sb.append(1 + " "); // for(int i = 0;i < 1;i++){ // sb.append(gen.nextInt(n)+1 + " "); // sb.append(gen.nextInt(n)+1 + " "); // } // INPUT = sb.toString(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new Q874_5().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }