#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

constexpr int MOD = 1000000007;

// + - * / ^
int mod(int a)
{
    int res = a % MOD;
    if(res < 0) {
        return res + MOD;
    }
    return res;
}
int mul_mod(int a, int b)
{
    ll res = ((ll)a * (ll)b) % MOD;
    return mod((int)res);
}
int pow_mod(int a, int b)
{
    ll res = 1;
    while (b > 0) {
        if(b & 1) {
            res = res * (ll)a % (ll)MOD;
        }
        a = mul_mod(a, a);
        b >>= 1;
    }
    return (int)res;
}
int inv_mod(int a)
{
    return pow_mod(a, MOD - 2);
}
int div_mod(int a, int b)
{
    return mul_mod(a, inv_mod(b));
}

// ! C
constexpr int FAC_MAX = 3000001;
ll fac[FAC_MAX], finv[FAC_MAX], inv[FAC_MAX];
void com_init()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < FAC_MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
ll com_mod(int a, int b){
    if (a < b) return 0;
    if (a < 0 || b < 0) return 0;
    return fac[a] * (finv[b] * finv[a - b] % MOD) % MOD;
}
ll p_mod(int a, int b){
    if (a < b) return 0;
    else return mul_mod(fac[a], finv[a - b]);
}

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    com_init();

    int n, m; cin >> n >> m;
    vector<P> p(n + m);
    rep (i, n + m) {
        cin >> p[i].first;
        p[i].second = i >= n;
    }
    sort(all(p));
    int i = 0, now = 0, hr = n, wr = m, prev = 0;
    ll ans = 1;
    while (i < n + m) {
        if (prev == p[i].first) {
            cout << "0\n";
            return 0;
        }
        prev = p[i].first;
        if (p[i].first == p[i + 1].first && p[i].second != p[i + 1].second) {
            eprintf("%d : %d %d : %lld\n", p[i].first, hr, wr, p_mod(p[i].first - 1 - now, hr + wr - 2));
            ans = mul_mod(ans, p_mod(p[i].first - 1 - now, hr + wr - 2));
            now += hr + wr - 1; hr--; wr--;i += 2;
        } else {
            if (p[i].second == 0) {
                eprintf("%d : - %d : %lld\n", p[i].first, wr, p_mod(p[i].first - 1 - now, wr - 1));
                ans = mul_mod(ans, mul_mod(wr, p_mod(p[i].first - 1 - now, wr - 1)));
                now += wr; hr--; i++;
            } else {
                eprintf("%d : %d - : %lld\n", p[i].first, hr, p_mod(p[i].first - now - 1, hr - 1));
                ans = mul_mod(ans, mul_mod(hr, p_mod(p[i].first - now - 1, hr - 1)));
                now += hr; wr--; i++;
            }
        }
    }
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 74004