#define rep(i,n) for (int i = 0; i < n; i++)
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <math.h>
#include <complex>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> p;
typedef complex<double> com;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const int inf = (1 << 31) - 1;


int main() {
	int n, m;
	cin >> n >> m;
	vector<int> a(n*m,0), b(n*m,0);
	int p;
	rep(i, n) {
		cin >> p;
		p--; a[p]++;
	}
	rep(i, m) {
		cin >> p;
		p--; b[p]++;
	}
	int x=0, y=0;
	ll ans = 1;
	rep(i, n*m) {
		int j = n * m - i - 1;
		if (!a[j] && !b[j]) {
			ans *= x * y - i;
		}
		else {
			if (a[j]) x++; 
          	if (b[j]) y++;
			if (a[j] && !b[j]) ans *= y;
			if (!a[j] && b[j]) ans *= x;
		}
		ans %= mod;
	}
	cout << ans << endl;
	return 0;
}