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

const long long mod = 1000000007;

signed main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n, m;
	cin >> n >> m;
	vector < int > a(n), b(m);
	for (int i = 0; i < n; ++i) 
		cin >> a[i];
	for (int i = 0; i < m; ++i)
		cin >> b[i];
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	bool ok = 1;
	for (int i = 1; i < n; ++i) 
		ok = ok && (a[i] != a[i - 1]);
	for (int i = 1; i < m; ++i)
		ok = ok && (b[i] != b[i - 1]);
	if (!ok)
		cout << "0\n";
	else {
		long long ans = 1;
		for (int i = n * m; i; --i) {
			int ja = lower_bound(a.begin(), a.end(), i) - a.begin();
			int jb = lower_bound(b.begin(), b.end(), i) - b.begin();
			if (ja == n || jb == m)
				ans = 0LL;
			else if (a[ja] == i && b[jb] == i)
				ans *= 1LL;
			else if (a[ja] == i)
				(ans *= ((long long) (m - jb))) %= mod;
			else if (b[jb] == i)
				(ans *= ((long long) (n - ja))) %= mod;
			else
				(ans *= (((long long) (n - ja)) * ((long long) (m - jb))) - ((long long) (n * m - i))) %= mod;
		}
		cout << ans << '\n';
	}
} 
