#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

void mod_inverse(int n, vector<long long>& inv, int mod)
{
    inv.assign(n+1, -1);
    inv[1] = 1;
    for(int i=2; i<=n; ++i)
        inv[i] = inv[mod % i] * (mod - mod / i) % mod;
}

class FactorialCalculation
{
private:
    const int mod;
    vector<long long> factorial;
    vector<long long> invFactorial;
public:
    FactorialCalculation(int n, int mod) : mod(mod)
    {
        factorial.resize(n+1, 1);
        invFactorial.resize(n+1, 1);
        vector<long long> inv;
        mod_inverse(n, inv, mod);
        for(int i=1; i<=n; ++i){
            factorial[i] = factorial[i-1] * i % mod;
            invFactorial[i] = invFactorial[i-1] * inv[i] % mod;
        }
    }
    long long getFactorial(int n){
        return factorial[n];
    }
    long long getInvFactorial(int n){
        return invFactorial[n];
    }
    long long getPermutation(int n, int r){
        if(n < r)
            return 0;
        return factorial[n] * invFactorial[n-r] % mod;
    }
    long long getCombination(int n, int r){
        if(n < r)
            return 0;
        return getPermutation(n, r) * invFactorial[r] % mod;
    }
    long long getHomogeneous(int n, int r){
        return getCombination(n+r-1, r);
    }
};

const int MOD = 1000000007;

long long solve(vector<int> a, vector<int> b)
{
    int n = a.size();
    int m = b.size();
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    a.push_back(INT_MAX);
    b.push_back(INT_MAX);
    for(int i=0; i<n-1; ++i){
        if(a[i] == a[i+1])
            return 0;
    }
    for(int i=0; i<m-1; ++i){
        if(b[i] == b[i+1])
            return 0;
    }

    FactorialCalculation fc(n*m, MOD);
    int rest = 0;
    int prev = 0;
    int i = 0;
    int j = 0;
    long long ans = 1;
    while(i < n || j < m){
        int cell = 0;
        if(a[i] == b[j]){
            rest += a[i] - prev - 1;
            prev = a[i];
            ++ i;
            ++ j;
            cell = (n - i) + (m - j);
        }
        else if(a[i] < b[j]){
            rest += a[i] - prev - 1;
            prev = a[i];
            ++ i;
            ans *= m - j;
            ans %= MOD;
            cell = m - j - 1;
        }
        else{
            rest += b[j] - prev - 1;
            prev = b[j];
            ++ j;
            ans *= n - i;
            ans %= MOD;
            cell = n - i - 1;
        }

        if(rest < cell)
            return 0;
        ans *= fc.getPermutation(rest, cell);
        ans %= MOD;
        rest -= cell;
    }
    return ans;
}

int main()
{
    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];

    cout << solve(a, b) << endl;

    return 0;
}


Measured memory_bytes: 23680