#include <iostream>
using namespace std;

const int N = 1005, mod = 1000 * 1000 * 1000 + 7;

int a[N];
int b[N];
bool markr[N * N];
bool markc[N * N];

int main() {
	int n, m, r = 0, c = 0;
	bool rp = false;
	long long ans = 1;
	cin >> n;
	cin >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (markr[a[i]])
			rp = true;
		markr[a[i]] = true;
	}
	for (int i = 1; i <= m; i++) {
		cin >> a[i];
		if (markc[a[i]])
			rp = true;
		markc[a[i]] = true;
	}
	if (rp)
		cout << 0 << endl;
	else {
		for (int i = n * m; i >= 1; i--) {
	//		cout << i << " " << markr[i] << " " << markc[i] << " " << ans << endl;
			if (markr[i]) {
				r++;
				if (markc[i]) {
		//			ans *= (n - r) * (m - c);
					c++;
				}
				else {
					ans *= c;
				}
			}
			else {
				if (markc[i]) {
					ans *= r;
					c++;
				}
				else {
					int x = r * c - n * m + i;
					ans *= x;
				}
			}
			ans %= mod;
		}
		cout << ans << endl;
	}

	return 0;
}
