#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double PI = 3.1415926535897932;   // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

template<int mod>
struct ModInt{
    int x;

    ModInt() : x(0) {}
    
    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    
    ModInt &operator+=(const ModInt &p){
        if((x += p.x) >= mod)   x -= mod;
        return *this;
    }
    
    ModInt &operator-=(const ModInt &p){
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator*=(const ModInt &p){
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    ModInt &operator/=(const ModInt &p){
        *this *= p.inverse();
        return *this;
    }
    
    ModInt operator-() const {return ModInt(-x);}

    ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;}

    ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;}

    ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;}

    ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;}

    bool operator==(const ModInt &p) const {return x == p.x;}

    bool operator!=(const ModInt &p) const {return x != p.x;}

    ModInt inverse() const{
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0){
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    ModInt pow(int64_t n) const{
        ModInt ret(1), mul(x);
        while(n > 0){
            if(n & 1)   ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ModInt &p){
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a){
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() {return mod;}
};

using modint = ModInt<mod>;

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

    int n, m;
    cin >> n >> m;
    vector<int> 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());
    sort(b.begin(), b.end());

    vector<int> LineSum(n * m + 1, 0), RowSum(n * m + 1, 0);
    vector<int> LineCnt(n * m + 1, 0), RowCnt(n * m + 1, 0);
    for(int i = 0; i < n; ++i){
        LineSum[a[i]] = 1;
        LineCnt[a[i]] = 1;
    }
    for(int i = n * m; i - 1 >= 0; --i) LineSum[i - 1] += LineSum[i];
    for(int i = 0; i < m; ++i){
        RowSum[b[i]] = 1;
        RowCnt[b[i]] = 1;
    }
    for(int i = n * m; i - 1 >= 0; --i) RowSum[i - 1] += RowSum[i];

    modint ans = 1;
    for(int k = n * m; k > 0; --k){
        if(LineCnt[k] & RowCnt[k])  continue;

        if(LineCnt[k] & (!RowCnt[k])){
            ans *= RowSum[k];
        }
        else if((!LineCnt[k]) & RowCnt[k]){
            ans *= LineSum[k];
        }
        else{   // ((!LineCnt[k]) & (!RowCnt[k]))
            if((!LineSum[k]) || (!RowSum[k])){
                cout << 0 << endl;
                return 0;
            }
            ans *= LineSum[k] * RowSum[k] - (n * m - k);
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 18740