#include <bits/stdc++.h>
using namespace std;
typedef long long int ll; typedef long double ld;
const int INF32 = 1e9+10;
const int MOD = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M; cin >> N >> M;
  int A[1010], B[1010];
  set<int> AS, BS;
  for (int i = 0; i < N; i++) {
    cin >> A[i]; AS.insert(A[i]);
  }
  if (AS.size() < N) {
    cout << 0 << endl; return 0;
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i]; BS.insert(B[i]);
  }
  if (BS.size() < M) {
    cout << 0 << endl; return 0;
  }
  sort(A,A+N);
  sort(B,B+M);
  ll ans = 1;
  for (int i = N*M; i >= 1; i--) {
    int r = 0;
    while (r < N && A[N-1-r] >= i)
      r++;
    int c = 0;
    while (c < M && B[M-1-c] >= i)
      c++;
    if (A[N-r] == i && B[M-c] == i) {
      ans *= 1;
    }
    else if (A[N-r] == i && B[M-c] != i) {
      ans *= c;
    }
    else if (A[N-r] != i && B[M-c] == i) {
      ans *= r;
    }
    else {
      if (r*c - (N*M-i) <= 0) {
        cout << 0 << endl; return 0;
      }
      ans *= (r*c - (N*M-i));
    }
    ans %= MOD;
  }
  cout << ans << endl;
}
