#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef vector<bool> vb;
 
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define repr(i,m,n) for(ll i=m;i>=n;i--)
#define all(x) x.begin(),x.end()
#define gll greater<ll>()
 
const ll mod=1e9+7;
ll n,m;
 
void solve(){
  cin >> n >> m;
  vll a(n),b(m);
  rep(i,0,n) cin >> a[i];
  rep(i,0,m) cin >> b[i];
  
  sort(all(a));
  sort(all(b));
  
  ll res=1;
  repr(x,n*m,1){
   ll s=upper_bound(all(a),x)-lower_bound(all(a),x);
   ll t=upper_bound(all(b),x)-lower_bound(all(b),x);
    
   if(s*t>1) {
     res*=0; 
     break; 
   }else if(s*t==1){
     res*=1; 
   }else if(s==1){
    res*=b.end()-lower_bound(all(b),x);
  }else if(t==1){
    res*=a.end()-lower_bound(all(a),x);
   }else {
    res*=((a.end()-lower_bound(all(a),x))*(b.end()-lower_bound(all(b),x))-n*m+x);
   }
    res%=mod;
  }
  cout << res << endl;
  }

int main(){
   cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}  