#include <bits/stdc++.h>
using namespace std;
int n, m, dc[1010101], dr[1010101], r, c, pot;
long long ans = 1;
void print() {
  puts("0");
  exit(0);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    if (dr[x]) print();
    dr[x] = 1;
  }
  for (int i = 1, x; i <= m; i++) {
    scanf("%d", &x);
    if (dc[x]) print();
    dc[x] = 1;
  }
  for (int i = n * m; i; i--) {
    if (dc[i] && dr[i]) {
      pot += c + r;
      c++;
      r++;
    } else if (dc[i]) {
      if (!r) print();
      (ans *= r) %= 1000000007;
      pot += r - 1;
      c++;
    } else if (dr[i]) {
      if (!c) print();
      (ans *= c) %= 1000000007;
      pot += c - 1;
      r++;
    } else {
      if (!pot) print();
      (ans *= pot) %= 1000000007;
      pot--;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
