#include <bits/stdc++.h>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

int main(){
    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 cntd=0, cntr=0;
    ll ans=1;
    for(ll i=n*m; i>=1; i--){
        if(cntd<n && cntr<m && a[cntd]==i && b[cntr]==i){
            cntd++, cntr++;
        }
        else if(cntd<n && a[cntd]==i){
            ans*=cntr;
            cntd++;
        }
        else if(cntr<m && b[cntr]==i){
            ans*=cntd;
            cntr++;
        }
        else{
            ans*=((cntd*cntr)-(n*m-i));
        }
        ans%=MOD;
    }
    cout << ans << endl;
}