#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll Mod = 1000000007;
bool exists(vector<ll>& vec,ll x)
{
    auto pos = lower_bound(vec.begin(),vec.end(),x);
    if(pos == vec.end())return false;
    return *pos == x;
}
void solve(ll N, ll M, vector<ll> A, vector<ll> B)
{
    ll ans = 1;
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    ll empty = 0;
    ll numrow=0,numcol=0;
    for(ll x = M*N;x>=1;--x)
    {
        if(exists(A,x))
        {
            if(exists(B,x))
            {
                empty += numcol + numrow;
                numcol++;
                numrow++;
            }else
            {
                empty += numcol - 1;
                ans *= numcol;
                ans %= Mod;
                numrow++;
            }
        }else
        {
            if(exists(B,x))
            {
                empty += numrow - 1;
                ans *= numrow;
                ans %= Mod;
                numcol++;
            }else
            {
                if(empty <= 0)
                {
                    cout<<0<<endl;
                    return;
                }
                ans *= empty;
                empty--;
                ans %= Mod;
            }
        }
    }
    cout<<ans<<endl;
}


int main()
{
    ll N;
    cin >> N;
    ll M;
    cin >> M;
    vector<ll> A(N);
    for(int i = 0 ; i < N ; i++)
    {
        cin >> A[i];
    }
    vector<ll> B(M);
    for(int i = 0 ; i < M ; i++)
    {
        cin >> B[i];
    }
    solve(N, M, move(A), move(B));
    return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

int N, M, A[1005], B[1005];
ll ans = 1;

int main(){
	getint(N), getint(M);
	for(register int i = 1; i <= N; i++) getint(A[i]);
	for(register int i = 1; i <= M; i++) getint(B[i]);
	sort(A + 1, A + N + 1), sort(B + 1, B + M + 1), A[N + 1] = B[M + 1] = inf;
	for(register int i = 1; i < N; i++) if(A[i] == A[i + 1]) return puts("0"), 0;
	for(register int i = 1; i < M; i++) if(B[i] == B[i + 1]) return puts("0"), 0;
	for(register int i = 1, j = 1, k = 1; i <= N * M; i++){
		while(j <= N && A[j] < i) j++;
		while(k <= M && B[k] < i) k++;
		if(A[j] == i && B[k] != i) ans = ans * (M - k + 1) % MOD;
		else if(A[j] != i && B[k] == i) ans = ans * (N - j + 1) % MOD;
		else if(A[j] != i && B[k] != i) ans = ans * ((M - k + 1) * (N - j + 1) - (N * M - i)) % MOD;
	}
	return printf("%lld\n", ans), 0;
}