#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main()
{
    int N, M; cin >> N >> M;
    vector<int> A(N*M+1);
    long long ans = 1, s = 0, t = 0;
    for (int i = 0; i < N; i++) {
        int a; cin >> a;
        if (A[a] & 1) goto ERROR;
        A[a] |= 1;
    }
    for (int i = 0; i < M; i++) {
        int a; cin >> a;
        if (A[a] & 2) goto ERROR;
        A[a] |= 2;
    }
    for (int i = N*M; i > 0; i--) {
        if (A[i] == 0) {
            ans = ans * (s * t - (N*M-i)) % MOD;
        } else if (A[i] == 1) {
            ans = ans * t % MOD;
        } else if (A[i] == 2) {
            ans = ans * s % MOD;
        } else {
            ans = ans;
        }
        if (A[i] & 1) s++;
        if (A[i] & 2) t++;
    }
    cout << ans << endl;
    return 0;
ERROR:
    cout << 0 << endl;
    return 0;
}