#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll n,m;
  cin >> n >> m;
  vec a(n*m+1),b(n*m+1);
  for(ll i=0;i<n;i++) {
    ll tmp;
    cin >> tmp;
    a[tmp]++;
  }
  for(ll i=0;i<m;i++) {
    ll tmp;
    cin >> tmp;
    b[tmp]++;
  }
  ll ans=1;
  for(ll i=n*m;i;i--) {
    if(a[i]>1||b[i]>1) {
      cout << 0 << endl;
      return 0;
    }
    if(!(a[i]*b[i])) {
      ll f=0;
      if(a[i]) f=1;
      if(b[i]) f=2;
      if(i!=n*m) {
        a[i]+=a[i+1];
        b[i]+=b[i+1];
      }
      if(f==1) {
        (ans*=b[i])%=mod;
      }
      else if(f==2) {
        (ans*=a[i])%=mod;
      }
      else {
        (ans*=(a[i]*b[i]%mod-(n*m-i)+mod)%mod)%=mod;
      }
    }
    else {
      if(i!=n*m) {
        a[i]+=a[i+1];
        b[i]+=b[i+1];
      } 
    }
  }
  cout << ans << endl;
}