#pragma once
#include <stdio.h>
#include <sstream>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <limits>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define bit(x,v) ((ll)x << v)

const ll INF = 1000000007;
const ll MAX = 1000010;
const ll MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (px < py) {
		pr[py] = px;
	}
	else {
		pr[px] = py;
	}
	return true;
}

// res[i][c] := i 文字目以降で最初に文字 c が登場する index (存在しないときは n)
vector<vector<int> > calcNext(const string & S) {
	int n = (int)S.size();
	vector<vector<int> > res(n + 1, vector<int>(26, n));
	for (int i = n - 1; i >= 0; --i) {
		for (int j = 0; j < 26; ++j) res[i][j] = res[i + 1][j];
		res[i][S[i] - 'a'] = i;
	}
	return res;
}
// mod 1000000007 の世界で a += b する関数
void add(long long& a, long long b) {
	a += b;
	if (a >= MOD) a -= MOD;
}
const ll ainf = 998244353;

struct cell {
	int x, y;
	int deep;
	cell(int x, int y, int deep) : x(x), y(y), deep(deep) {}
};

int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };

int W, H, N;
int X1[500], X2[500], Y1[500], Y2[500];

bool fld[500 * 6][500 * 6];
int compress(int* x1, int* x2, int w) {
	vector<int> xs;
	for (int i = 0; i < N; i++)
	{
		for (int d = -1; d <= 1; d++)
		{
			int tx1 = x1[i] + d, tx2 = x2[i] + d;
			if (1 <= tx1 && tx1 <= W) xs.push_back(tx1);
			if (1 <= tx2 && tx2 <= W) xs.push_back(tx2);
		}
	}

	sort(xs.begin(), xs.end());
	xs.erase(unique(xs.begin(), xs.end()), xs.end());

	for (size_t i = 0; i < N; i++)
	{
		x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
		x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
	}
	return xs.size();
}
const int MAX_N = 1 << 17;
//
//int n,dat[2 * MAX_N - 1];
//
//void init(int n_) {
//	n = 1;
//	while (n < n_)
//	{
//		n *= 2;
//	}
//	for (size_t i = 0; i < 2 * n - 1; i++)
//	{
//		dat[i] = INT_MAX;
//	}
//}
//
//void update(int k, int a) {
//	k += n - 1;
//	dat[k] = a;
//	while (k > 0) {
//		k = (k - 1) / 2;
//		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
//	}
//}
//int query(int a, int b, int k, int l, int r) {
//	if (r <= a || b <= 1) return INT_MAX;
//
//	if (a <= l && r <= b) return dat[k];
//	else {
//		int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
//		int vr = query(a, b, k * 2 + 1, (l + r) / 2, r);
//		return min(vl, vr);
//	}
//	
//	auto p = tie(a,b,k);
//}
ll fmemo[1000010];
ll fact(ll x) {
	if (fmemo[x] > 0) return fmemo[x];
	if (x == 0) return 1;
	fmemo[x] = (x * fact(x - 1));
	fmemo[x] %= INF;
	return fmemo[x];
}
void solv() {
	ll n, m;
	cin >> n >> m;
	ll a[1010];
	ll ai[1000010];
	ll b[1010];
	ll bi[1000010];
	memset(ai, -1, sizeof(ai));
	memset(bi, -1, sizeof(bi));
	ll cnt = n * m;
	ll cnt2 = n * m;
	for (size_t i = 0; i < n; i++) {
		cin >> a[i];
		ai[a[i]] = i;
	}


	ll tb[1010][1010];
	memset(tb, 0, sizeof(tb));
	ll tc[1000010];
	memset(tc, 0, sizeof(tc));

	for (int i = 0; i < m; i++)
	{
		cin >> b[i];
		bi[b[i]] = i;
		if (ai[b[i]] >= 0) {
			tb[ai[b[i]]][bi[b[i]]] = b[i];
			cnt--;
			cnt2--;
		}
	}

	for (ll i = 1; i <= n * m; i++)
	{
		if (ai[i] >= 0 && bi[i] >= 0) {

			for (size_t j = 0; j < n; j++)
			{
				if (tb[j][bi[i]] == 0) { 
					tb[j][bi[i]] = i;
					tc[i]++;
				}
			}

			for (size_t j = 0; j < m; j++)
			{
				if (tb[ai[i]][j] == 0) {
					tb[ai[i]][j] = i;
					tc[i]++;
				}
			}

		}else if(ai[i] >= 0){

			for (size_t j = 0; j < m; j++)
			{
				if (tb[ai[i]][j] == 0) {
					tb[ai[i]][j] = i;
					tc[i]++;
				}
			}
		}
		else if (bi[i] >= 0) {
			for (size_t j = 0; j < n; j++)
			{
				if (tb[j][bi[i]] == 0) {
					tb[j][bi[i]] = i;
					tc[i]++;
				}
			}
		}
	}

	//// 行を処理
	//for (int i = 0; i < n; i++)
	//{
	//	ll v = a[i];
	//	if (bi[v] >= 0) continue;

	//	for (int j = 0; j < m; j++)
	//	{
	//		if (v <= b[j]) {
	//			tc[v]++;
	//			tb[i][j] = v;
	//			cnt--;
	//		}
	//	}
	//}

	//// 列を処理
	//for (int i = 0; i < m; i++)
	//{
	//	ll v = b[i];
	//	if (ai[v] >= 0) continue;

	//	for (int j = 0; j < n; j++)
	//	{
	//		if (v <= a[j]) {
	//			tc[v]++;
	//			tb[j][i] = v;
	//			cnt--;
	//		}
	//	}
	//}

	ll rem = 0;
	ll res = 1;
	ll mc = 0;
	for (ll i = 1; i <= n * m; i++)
	{
		ll cn = tc[i];

		bool fix = ai[i] >= 0 && bi[i] >= 0;
		if (cn == 0 && !fix) {
			mc++;
			continue;
		}
		if (!fix) {
			res *= cn;
			res %= INF;
		}
		ll rv = (cn - !fix);
		//rem = rv - (cn - (!fix));
		res *= COM(mc, rv);
		res %= INF;
		res *= fact(rv);
		res %= INF;
		mc = mc - (rv);
	}

	if (mc != 0) {
		cout << 0 << endl;
	}
	else {
		res *= fact(mc);
		res %= INF;
		cout << res << endl;
	}
}

int main() {
	COMinit();
	solv();

	return 0;
}


Measured memory_bytes: 55808