#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

int N, M, A[1005], B[1005];
ll ans = 1;

int main(){
	getint(N), getint(M);
	for(register int i = 1; i <= N; i++) getint(A[i]);
	for(register int i = 1; i <= M; i++) getint(B[i]);
	sort(A + 1, A + N + 1), sort(B + 1, B + M + 1), A[N + 1] = B[M + 1] = inf;
	for(register int i = 1; i < N; i++) if(A[i] == A[i + 1]) return puts("0"), 0;
	for(register int i = 1; i < M; i++) if(B[i] == B[i + 1]) return puts("0"), 0;
	for(register int i = 1, j = 1, k = 1; i <= N * M; i++){
		while(j <= N && A[j] < i) j++;
		while(k <= M && B[k] < i) k++;
		if(A[j] == i && B[k] != i) ans = ans * (M - k + 1) % MOD;
		else if(A[j] != i && B[k] == i) ans = ans * (N - j + 1) % MOD;
		else if(A[j] != i && B[k] != i) ans = ans * ((M - k + 1) * (N - j + 1) - (N * M - i)) % MOD;
	}
	return printf("%lld\n", ans), 0;
}
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define dbg(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

ll ca[1111111]={};
ll cb[1111111]={};

int main(){
  ll n,m,p=1e9+7;
  cin>>n>>m;

  ll f=0;
  for(int i=0;i<n;i++){
    ll a;
    cin>>a;
    ca[a]++;
    if(ca[a]==2)f=1;
  }
  for(int i=0;i<m;i++){
    ll b;
    cin>>b;
    cb[b]++;
    if(cb[b]==2)f=1;
  }
  
  ll nm=n*m;
  if(ca[nm]==0||ca[nm]==0||f){
    cout<<0<<endl;
    return 0;
  }

  ll cr=0,cc=0;
  ll ans=1;
  ll c0=0;
  for(int i=nm;i>0;i--){
    //    cout<<ans<<" "<<cr<<" "<<cc<<endl;
    if(ca[i]&&cb[i]){
	 cc++;
	 cr++;
	 c0++;
	 continue;
    }
    if(ca[i]){
	 ans*=cc;
	 ans%=p;
	 cr++;
	 c0++;
	 continue;
    }
    if(cb[i]){
	 ans*=cr;
	 ans%=p;
	 cc++;
	 c0++;
	 continue;
    }
    ans*=(cr*cc-c0);
    ans%=p;
    c0++;
    continue;
  }

  cout<<ans<<endl;
  return 0;
}
