#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cassert>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define mp make_pair
#define fi first
#define se second
#define pb push_back

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

//########################
//組み合わせ
//TODO:fastPow必須
//TODO:combInit()実行
//########################
//TODO:数が大きい場合は適宜修正

ll fastPow(ll x, ll n)
{
    if (n == 0)
        return 1;
    if (n % 2 == 0)
        return fastPow(x * x % MOD, n / 2);
    else
        return x * fastPow(x, n - 1) % MOD;
}
ll fac[112345];
void combInit(int mx)
{
    fac[0] = 0;
    fac[1] = 1;
    for (int i = 2; i <= mx; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
    }
}

ll modDiv(ll a, ll b)
{
    return a * fastPow(b, MOD - 2) % MOD;
}

ll comb(ll a, ll b)
{
    if (a < b)
        return 0;
    if (a <= 0 || b < 0)
        return 0;
    if (a == b)
        return 1;

    ll p, c;

    c = modDiv(fac[a], fac[a - b]);
    p = fac[b];

    ll res = modDiv(c, p);

    return res;
}

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

    int n, m, a[1100], b[1100];
    ll kai[1123456];

    kai[0] = 1;
    kai[1] = 1;
    REP(i, 1000000)
    kai[i + 2] = kai[i + 1] * (i + 2) % MOD;

    cin >> n >> m;
    REP(i, n)
    cin >> a[i];
    REP(i, m)
    cin >> b[i];

    sort(a, a + n);
    sort(b, b + m);

    REP(i, n - 1)
    {
        if (a[i] == a[i + 1])
        {
            cout << 0 << endl;
            return 0;
        }
    }

    REP(i, m - 1)
    {
        if (b[i] == b[i + 1])
        {
            cout << 0 << endl;
            return 0;
        }
    }

    ll res = 1;

    int i = 0;
    int j = 0;
    int di = 0;
    int dj = 0;

    int num = 0;

    while (i < n && j < m)
    {
        if (a[i] == b[j])
        {
            //cout << "#" << modDiv(kai[a[i] - 1 - num], kai[(a[i] - 1 - num) - (n - di - 1)]) << endl;
            //cout << "$" << a[i] << " " << num << " " << di << endl;

            if (b[j] - 1 - num < 0)
            {
                cout << 0 << endl;
                return 0;
            }

            res *= modDiv(kai[b[j] - 1 - num], kai[(b[j] - 1 - num) - (n - di - 1)]);
            res %= MOD;
            num += n - di;
            dj++;

            //cout << "#" << modDiv(kai[a[i] - num], kai[(a[i] - num) - (m - dj - 1)]) << endl;

            if (a[i] - num < 0)
            {
                cout << 0 << endl;
                return 0;
            }

            res *= modDiv(kai[a[i] - num], kai[(a[i] - num) - (m - dj)]);
            res %= MOD;
            num += m - dj;
            di++;

            i++;
            j++;
        }
        else if (a[i] < b[j])
        {
            if (a[i] - 1 - num < 0)
            {
                cout << 0 << endl;
                return 0;
            }

            res *= m - dj;
            res %= MOD;
            res *= modDiv(kai[a[i] - 1 - num], kai[(a[i] - 1 - num) - (m - dj - 1)]);
            res %= MOD;

            num += m - dj;
            i++;
            di++;
        }
        else
        {
            if (b[j] - 1 - num < 0)
            {
                cout << 0 << endl;
                return 0;
            }

            res *= n - di;
            res %= MOD;
            res *= modDiv(kai[b[j] - 1 - num], kai[(b[j] - 1 - num) - (n - di - 1)]);
            res %= MOD;

            num += n - di;
            j++;
            dj++;
        }
        // cout << res << endl;
    }

    cout << res << endl;

    return 0;
}

Measured memory_bytes: 8960