#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

class comb{
public:
    ll mod;
    ll mx;
    vector<ll> F;
    vector<ll> FR;
    
public:
    comb(ll mod=1000000007,ll mx=100000):mod(mod),mx(mx),F(mx+1,1),FR(mx+1,1){
        mk_F();
    }
    
    ll mod_pow(ll a,ll x){
        a%=mod;
        ll ans=1;
        while(x>0){
            if(x&1){ans*=a; ans%=mod;}
            a*=a;
            a%=mod;
            x>>=1;
        }
        return ans;
    }
    
    pll Ex_gcd(ll a,ll b){
        if(b==0){return {1,0};}
        pll ret=Ex_gcd(b,a%b);
        ret.F-=a/b*ret.S;
        return {ret.S,ret.F};
    }
    
    ll prime_R(ll a){
        return mod_pow(a,mod-2);
    }
    
    ll R(ll a){
        ll ret=Ex_gcd(a,mod).F;
        ret%=mod;
        if(ret<0){ret+=mod;}
        return ret;
    }
    
    void mk_F(){
        for(ll i=1;i<=mx;i++){F[i]=F[i-1]*i%mod; FR[i]=R(F[i]);}
    }
    
    ll c(ll n,ll k){
        if(n<k){return 0;}
        if(n==k || k==0){return 1;}
        if(k<0){return 0;}
        return F[n]*FR[n-k]%mod*FR[k]%mod;
    }
    
    //mod must be prime
    ll Lucas_C(ll n,ll m){
        ll ret=1;
        while(n>0 || m>0){
            ret*=c(n%mod,m%mod);
            ret%=mod;
            n/=mod; m/=mod;
        }
        return ret;
    }
    
    ll Stirling(ll n,ll k){
        ll ret=0;
        for(ll i=1;i<=k;i++){
            if((k-i)%2){ret-=c(k,i)*mod_pow(i,n)%mod;}
            else{ret+=c(k,i)*mod_pow(i,n)%mod;}
            ret%=mod;
        }
        ret*=R(F[k]);
        ret%=mod;
        if(ret<0){ret+=mod;}
        return ret;
    }
    
    ll Bell(ll n,ll k){
        ll ret=0;
        for(ll i=1;i<=k;i++){ret+=Stirling(n,i); ret%=mod;}
        return ret;
    }
};


int main(){
    comb C(MOD,2000001);
    ll n,m;
    cin>>n>>m;
    vector<ll> a(n),b(m);
    for(int i=0;i<n;i++){cin>>a[i];}
    for(int i=0;i<m;i++){cin>>b[i];}
    sort(a.begin(),a.end(),greater<ll>());
    sort(b.begin(),b.end(),greater<ll>());
    ll ans=1;
    ll A=0,B=0;
    ll used=0;
    ll mx=n*m;
    vector<ll> CA(n,0);
    vector<ll> CB(m,0);
    while(A<n || B<m){
        ll rem=A*B-used;
        //cout<<rem<<" "<<a[A]<<" "<<b[B]<<" "<<A<<" "<<B<<" "<<ans<<" "<<used<<endl;
        if(A<n && B<m){
            if(mx!=max(a[A],b[B])){
                if(mx-max(a[A],b[B])<0){ans=0; break;}
                ans*=C.c(rem,mx-max(a[A],b[B]))*C.F[mx-max(a[A],b[B])]%MOD;
                ans%=MOD;
                used+=mx-max(a[A],b[B]);
                mx=max(a[A],b[B]);
            }
            else if(a[A]==b[B]){
                CA[A]--;
                CB[B]--;
                used++;
                A++; B++;
                mx--;
            }
            else if(a[A]>b[B]){
                used++;
                ll cnt=B+CA[A];
                (ans*=cnt)%=MOD;
                mx--;
                A++;
            }
            else{
                used++;
                ll cnt=A+CB[B];
                (ans*=cnt)%=MOD;
                mx--;
                B++;
            }
        }
        else if(A<n){
            if(a[A]!=mx){
                if(mx-a[A]<0){ans=0; break;}
                ans*=C.c(rem,mx-a[A])*C.F[mx-a[A]]%MOD;
                ans%=MOD;
                used+=mx-a[A];
                mx=a[A];
                continue;
            }
            used++;
            ll cnt=B+CA[A];
            (ans*=cnt)%=MOD;
            mx--;
            A++;
        }
        else{
            if(b[B]!=mx){
                if(mx-b[B]<0){ans=0; break;}
                ans*=C.c(rem,mx-b[B])*C.F[mx-b[B]]%MOD;
                used+=mx-b[B];
                ans%=MOD;
                mx=b[B];
                continue;
            }
            used++;
            ll cnt=A+CB[B];
            (ans*=cnt)%=MOD;
            mx--;
            B++;
        }
    }
    //cout<<mx<<endl;
    ans*=C.F[mx];
    ans%=MOD;
    if(ans<0){ans+=MOD;}
    cout<<ans%MOD<<endl;
    
    
    
    return 0;
}


Measured memory_bytes: 31488