#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e6+5,mod=1e9+7;
int r[N],c[N];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;cin>>n>>m;
    for(ll i=1;i<=n;i++){
        ll x;cin>>x;
        if(r[x])return cout<<"0" , 0;
        r[x]=i;
    }
    for(ll i=1;i<=m;i++){
        ll x;cin>>x;
        if(c[x])return cout<<"0" , 0;
        c[x]=i;
    }
    ll ans=1,x=0,y=0;
    for(ll val=n*m;val>=1;val--){
        if(r[val]||c[val]){
            if(r[val]&&c[val])x++,y++;
            else if(r[val]){
                ans*=y;
                ans%=mod;
                x++;
            }
            else{
                ans*=x;
                ans%=mod;
                y++;
            }
        }
        else{
            ll tmp= x*y - (n*m - val);
            ans*=tmp;
            ans%=mod;
        }
    }
    cout<<ans;
}