#include<bits/stdc++.h>
using namespace std;
const int N=1050;
int a[N],b[N],col,n,m,com,tot;
const long long mod=1e9+7;
long long ans=0;
bool vis[2][N*N];
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vis[0][a[i]]=true;
	}
	for(int j=1;j<=m;j++) {
		scanf("%d",&b[j]);
		vis[1][b[j]]=true;
	}
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	ans=1;
	for(int i=1;i<n;i++) if(a[i]==a[i+1]) ans=0;
	for(int j=1;j<m;j++) if(b[j]==b[j+1]) ans=0;
	col=n;com=m;
	for(int i=m*n;i>=1;i--){
		while(col>=1&&a[col]>=i) col--;
		while(com>=1&&b[com]>=i) com--;
		tot=(n-col)*(m-com)-(n*m-i);
		if(tot<=0) ans=0;
		if(vis[0][i]&&vis[1][i]) tot=1;
		else if(vis[0][i]) tot=m-com;
		else if(vis[1][i]) tot=n-col;
		ans=ans*tot%mod;
	}
	printf("%lld\n",ans);
	
	
}
#include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main(){
  int n,m;cin>>n>>m;
  vector<int>a(n), b(m);
  rep(i, n)cin>>a[i];
  rep(i, m)cin>>b[i];

  map<int, int>ma, mb;
  for(auto c:a)ma[c]++;
  for(auto p: ma)if(p.second>1) { cout<<0<<endl; return 0; }
  for(auto c:b)mb[c]++;
  for(auto p: mb)if(p.second>1) { cout<<0<<endl; return 0; }

  vector<int> counter_a(n*m+1);
  vector<int> counter_b(n*m+1);
  for(auto c: a)counter_a[c]++;
  for(auto c: b)counter_b[c]++;
  rrep1(i, n*m)counter_a[i-1] += counter_a[i];
  rrep1(i, n*m)counter_b[i-1] += counter_b[i];

  ll result=1;
  rrep1(c, n*m){
    ll cca = counter_a[c];
    ll ccb = counter_b[c];
    ll reduce = (n*m-c);

    if(ma[c]>0 && mb[c]>0);
    else if(ma[c]>0){
      result = result*ccb%MOD;
    }else if(mb[c]>0){
      result = result*cca%MOD;
    }else{
      result = result*(cca*ccb-reduce)%MOD;
    }
  }

  cout<<result<<endl;

  return 0;
}
