#include<cstdio>
#define Rint register int
using namespace std;
typedef long long LL;
const int N = 1000003, mod = 1e9 + 7;
int n, m;
bool col[N], row[N];
int main(){
	scanf("%d%d", &n, &m);
	for(Rint i = 1;i <= n;i ++){
		int x;
		scanf("%d", &x);
		if(col[x]){puts("0"); return 0;}
		col[x] = true;
	}
	for(Rint i = 1;i <= m;i ++){
		int x;
		scanf("%d", &x);
		if(row[x]){puts("0"); return 0;}
		row[x] = true;
	}
	int ans = 1, c = 0, r = 0;
	for(Rint i = n * m;i;i --){
		int tc = c, tr = r;
		if(col[i]){tc = 1; ++ c;}
		if(row[i]){tr = 1; ++ r;}
		if(col[i] || row[i]) ans = (LL) ans * tc % mod * tr % mod;
		else ans = (LL) ans * (tc * tr - n * m + i) % mod;
	}
	printf("%d\n", ans);
}