#include <bits/stdc++.h>

using namespace std;

#define FORE(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define REPU(i, a, b) for (int i = (a); i < (b); ++i)
#define REPD(i, a, b) for (int i = (a); i > (b); --i)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())

vector<string> split(const string &s, char c) {
	vector<string> v; stringstream ss(s); string x;
	while (getline(ss, x, c)) v.push_back(x);
	return v;
}
#define DEBUG(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); }
void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
	cerr << "[DEBUG] " << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << '\n';
	err(++it, args...);
}

typedef long long ll;
const int MOD = 1000000007;

template<class T, class U> inline T tmin(T a, U b) { return (a < b) ? a : b; }
template<class T, class U> inline T tmax(T a, U b) { return (a > b) ? a : b; }
template<class T, class U> inline void amax(T &a, U b) { if (b > a) a = b; }
template<class T, class U> inline void amin(T &a, U b) { if (b < a) a = b; }
template<class T> T gcd(T a, T b) { while (b != 0) { T c = a; a = b; b = c % b; } return a; }

const int N = 1005;
ll ft[N * N], inv[N * N];

struct E {
	int v, t;
};

ll mod_pow(ll x, ll y, ll p) {
    ll res = 1;
    while (y > 0) {
        if (y & 1) res = res * x % p;
        x = x * x % p;
        y >>= 1;
    }
    return res;
}

inline ll comb(int k, int n) {
	if (k > n || n < 0) return 0;
	ll res = inv[k] * inv[n - k] % MOD;
	res = res * ft[n] % MOD;
	return res;
}

inline ll pf(int k, int n) {
	if (k > n || n < 0) return 0;
	ll res = ft[n] * inv[n - k] % MOD;
	return res;
}

int main(int argc, char *argv[]) {
	ios_base::sync_with_stdio(false);

	int n, m; cin >> n >> m;
	vector<E> es;

	REPU(i, 0, n) {
		int a; cin >> a;
		es.push_back((E) {a, 0});
	}
	REPU(i, 0, m) {
		int b; cin >> b;
		es.push_back((E) {b, 1});
	}

	sort(ALL(es), [](E e1, E e2) {
		if (e1.v != e2.v) return e1.v < e2.v;
		return e1.t < e2.t;
	});

	ft[0] = ft[1] = inv[0] = inv[1] = 1;
	REPU(i, 2, n * m + 1) ft[i] = ft[i - 1] * i % MOD;
	inv[n * m] = mod_pow(ft[n * m], MOD - 2, MOD);
	REPD(i, n * m - 1, 1) {
		inv[i] = (i + 1) * inv[i + 1] % MOD;
	}

	int nr = 0, nc = 0;
	ll ans = 1;
	for (int i = 0; i < n + m;) {
		int v = es[i].v;
		int used = nr * m + nc * n - nr * nc;
		if (i + 1 < n + m && es[i].t == es[i + 1].t && es[i].v == es[i + 1].v) {
			ans = 0; break;
		}
		if (!es[i].t) {
			if (i + 1 < n + m && es[i + 1].t && es[i + 1].v == es[i].v) {
				int rem = v - 1 - used;
				if (rem < 0) {
					puts("0"); return 0;
				}
				ans = ans * pf(n + m - 2 - nr - nc, rem) % MOD;
				nr++, nc++;
				i += 2;
			}
			else {
				int rem = v - 1 - used;
				if (rem < 0) {
					puts("0"); return 0;
				}
				ans = ans * comb(m - nc - 1, rem) % MOD;
				ans = ans * ft[m - nc] % MOD;
				nr++;
				i += 1;
			}
		}
		else {
			int rem = v - 1 - used;
			if (rem < 0) {
				puts("0"); return 0;
			}
			ans = ans * comb(n - nr - 1, rem) % MOD;
			ans = ans * ft[n - nr] % MOD;
			nc++;
			i += 1;
		}
		if (ans == 0) break;
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 16000