#include <vector>
#include <iostream>
#include <cmath>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include <string>
#include <numeric>
#include <queue>
#include <deque>
#include <sstream>
#include <iomanip>
#include <set>
#include <stack>
#include <cassert>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> vp;
typedef vector<PL> vpl;
typedef vector<vector<P>> vvp;
typedef vector<vector<PL>> vvpl;
const int INF = 1001001001;
const ll LINF = 1e16;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for(ll i = (a); i < b; i++)
#define RFOR(i, a, b) for(ll i = (a); i > b; i--)
#define REP(i, n) for(ll i = 0; i < n; i++)
#define RREP(i, n) for(ll i = n-1; i > -1; i--)
#define FORMAP(it, m) for(auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back
#define epb emplace_back

template <typename T>
T gcd(T a, T b) {
    return (a == 0) ? b : gcd(b%a, a);
}
template <typename T>
T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}

bool p_comp_fs(const PL p1, const PL p2){ return p1.first < p2.first;};
bool p_comp_fg(const PL p1, const PL p2){ return p1.first > p2.first;};
bool p_comp_ss(const PL p1, const PL p2){ return p1.second < p2.second;};
bool p_comp_sg(const PL p1, const PL p2){ return p1.second > p2.second;};
template <typename T>
vector<T> uniquen(vector<T> vec){
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    return vec;
}


int main(){
    ll N, M; cin >> N >> M;
    vl A(N); REP(i, N) cin >> A[i];
    vl B(M); REP(i, M) cin >> B[i];
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    auto ua = uniquen(A);
    auto ub = uniquen(B);
    if(ua.size() != N || ub.size() != M){
        cout << 0 << endl; return 0;
    }

    set<ll> uq;
    REP(i, N) uq.insert(A[i]); REP(i, M) uq.insert(B[i]);
    
    ll mod = 1e9+7;
    ll ans = 1;
    REP(i, N){
        ll a = A[i];
        auto it = std::lower_bound(B.begin(), B.end(), a);
        if(it == B.end()) ans *= 0;
        else if(*it == a) continue;
        else{
            ans = ans * (M - (it-B.begin())) % mod;
        }
    }
    
    REP(i, M){
        ll b = B[i];
        auto it = std::lower_bound(A.begin(), A.end(), b);
        if(it == A.end()) ans *= 0;
        else if(*it == b) continue;
        else{
            ans = ans * (N - (it-A.begin())) % mod;
        }
    }
    
    FOR(i, 1, N*M+1){
        if(uq.count(i) == 0){
            auto ita = std::lower_bound(A.begin(), A.end(), i);
            auto itb = std::lower_bound(B.begin(), B.end(), i);
            ll c = (N - (ita-A.begin())) * (M - (itb-B.begin())) - (N*M-i);
            if(c <= 0) ans = 0;
            else ans = ans*c % mod;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 384