import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


immutable MO = 10L^^9 + 7;

int M, N;
int[] A, B;

long solve() {
  A.sort();
  B.sort();
  A.reverse();
  B.reverse();
  debug {
    writefln("A = %s", A);
    writefln("B = %s", B);
  }
  
  auto xs = new int[M * N];
  auto ys = new int[M * N];
  xs[] = -1;
  ys[] = -1;
  foreach (x; 0 .. M) {
    if (xs[A[x]] != -1) {
      return 0;
    }
    xs[A[x]] = x;
  }
  foreach (y; 0 .. N) {
    if (ys[B[y]] != -1) {
      return 0;
    }
    ys[B[y]] = y;
  }
  
  long ans = 1;
  int cx, cy;
  foreach_reverse (i; 0 .. M * N) {
    for (; cx < M && i <= A[cx]; ++cx) {}
    for (; cy < N && i <= B[cy]; ++cy) {}
    long here;
    if (xs[i] != -1) {
      if (ys[i] != -1) {
        here = 1;
      } else {
        here = cy;
      }
    } else {
      if (ys[i] != -1) {
        here = cx;
      } else {
        here = cx * cy - (M * N - 1 - i);
      }
    }
    debug {
      writefln("i = %s: cx = %s, cy = %s, here = %s", i, cx, cy, here);
    }
    (ans *= here) %= MO;
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      M = readInt();
      N = readInt();
      A = new int[M];
      B = new int[N];
      foreach (x; 0 .. M) {
        A[x] = readInt() - 1;
      }
      foreach (y; 0 .. N) {
        B[y] = readInt() - 1;
      }
      const ans = solve();
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
