#include <bits/stdc++.h>
#define pb push_back
#define S second
#define F first

using namespace std;
typedef long long ll;
#define int ll
typedef pair<int,int> pii;
const int maxn = 1e6 + 10;
const int mod= 1e9+7;
int a[maxn];
int32_t main(){
   int n,m;
   cin >> n >> m;
   int ans = 1;
   for (int i = 1;i <=n;i++){
        int b;
        cin >> b;
        a[b]++;
        if (a[b] > 1) ans = 0;
   }
   for (int i = 1;i <= m;i++){
        int b;
        cin >> b;
        a[b]+= 2;
        if (a[b] > 3) ans = 0;
   }
   int col = 0,row= 0,cnt = 1;
   if (a[n *m ] != 3) ans = 0;
   else {
        row++,col++;
   }

   for (int i = n * m - 1;i;i--){
        if (a[i] == 3){
            row++,col++;
        }else if (a[i] == 2){
            ans *= (row);ans %= mod;
            col++;
        }else if (a[i] == 1){
            ans *= (col);ans%= mod;
            row++;
        }else {
            int can = row * col - cnt;
            if (can <= 0) ans = 0;
            else ans = (ans * can) % mod;
        }
        cnt++;
   }
   cout << ans << endl;

}
