#include<iostream>
#include<algorithm>
#include<functional>

using namespace std;
typedef long long ll;

int H, W;
ll MOD = 1000000007;
int A[1001], B[1001];

int main() {
	cin >> H >> W;
	for (int i = 0; i < H; i++)cin >> A[i];
	for (int i = 0; i < W; i++)cin >> B[i];
	sort(A, A + H + 1, greater<int>()), sort(B, B + W + 1, greater<int>());
	ll ans = 1;
	int Apos = 0, Bpos = 0;
	for (int n = H * W; n > 0; n--) {
		ll tmp = 1;
		if (n == A[Apos] && n == B[Bpos]) {
			Apos++, Bpos++;
		}
		else if (n == A[Apos]) {
			tmp *= Bpos;
			Apos++;
		}
		else if (n == B[Bpos]) {
			tmp *= Apos;
			Bpos++;
		}
		else {
			tmp *= (Apos * Bpos - (H * W - n));
		}
		ans *= tmp;
		ans %= MOD;
	}
	cout << ans << endl;
	return 0;
}