#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int mod = 1e9 + 7;

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);

  int n, m;
  cin >> n >> m;
  vector < int > a (n + 1), b (m + 1);
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int j = 1; j <= m; j++) cin >> b[j];
  sort(a.begin() + 1, a.end());
  sort(b.begin() + 1, b.end());

  ll ans = 1;
  int p = n, q = m;
  for (int val = n * m, filled = 0; val > 0; val--, filled++) {
    if (a[p] == val && b[q] == val) {
      p--;
      q--;
    } else if (a[p] == val) {
      ans = (ans * (m - q)) % mod;
      p--;
    } else if (b[q] == val) {
      ans = (ans * (n - p)) % mod;
      q--;
    } else {
      ans = (ans * ((n - p) * (m - q) - filled)) % mod;
    }
  }

  cout << ans << '\n';
}
