#include <bits/stdc++.h>

using namespace std;
#define fori(n) for(int i=0;i<n;i++)
#define ll long long

int main()
{
    //freopen("dulieu","r",stdin);
    int n,m,x;
    ll kq=1,mod=1000000007;
    int sc=0,sh=0;
    bool ax,bx;
    cin>>n>>m;
    set<int> A,B;
    fori(n)
    {
        cin >> x;
        A.insert(x);
    }
    fori(m)
    {
        cin >> x;
        B.insert(x);
    }
    int sl=0;
    for (int x=n*m; x>0; x--)
    {
        ax=A.count(x)>0;
        bx=B.count(x)>0;
        if(ax)sh++;
        if(bx)sc++;
        if(ax)
        {
            if(!bx) kq=kq*sc % mod;
        }
        else
        {
            if (bx) kq=kq*sh % mod;
            else kq=kq*(sc*sh-sl) % mod;
        }
        sl++;
        //if (sl>10) break; cout <<x<<" "<< kq<<endl;
    }
    cout << kq;
    return 0;
}
