#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
//#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef vector< vvi > vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;
typedef complex<int> point;

#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for(int i = 0; i < int(n); ++i)
#define SZ(a) ((int)((a).size()))
#define triple(T) tuple<T, T, T>
#define quad(T) tuple<T, T, T, T>

#ifdef MAX_HOME
#define cerr cout
#else
#define cerr if (false) cerr
#endif

#define watch(x) cerr << (#x) << " = " << (x)    << endl;

const double PI = 2 * acos(0.0);

const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";

template <class T0, class T1>
inline ostream & operator << (ostream &out, pair<T0, T1> &a) {
    return out << "{" << a.first << ", " << a.second << "}";
}

template <class T0, class T1, class T2>
inline ostream & operator << (ostream &out, tuple<T0, T1, T2> &a) {
    return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}";
}

template <class T0, class T1, class T2, class T3>
inline ostream & operator << (ostream &out, tuple<T0, T1, T2, T3> &a) {
    return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " <<  get<3>(a) << "}";
}

template<class T>
inline ostream & operator << (ostream &out, vector<T> &a) {
    out << "[";
    fori (i, a.size())
        out << a[i] << vector<string>{", ", "]  "}[i + 1 == a.size()];
    return out;
}



void smain();

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

#ifdef MAX_HOME
    freopen("input.txt", "r", stdin);
    clock_t start = clock();
#endif
    cout << setprecision(12) << fixed;
    smain();
#ifdef MAX_HOME
    cout << "\n\n\n\nTOTAL EXECUTION TIME: " << float( clock () - start ) /  CLOCKS_PER_SEC << endl;
#endif
}

namespace naive {
    ll solve(vl a, vl b) {
        int n = a.size();
        int m = b.size();
        vl kek(n * m);
        fori (i, n * m) kek[i] = i + 1;
        ll ans = 0;
        do {
            vector<char> fa(n, 0), fb(m, 0);
            bool ok = 1;
            for (int i = 0; ok && i < n; ++i) {
                for (int j = 0; ok && j < m; ++j) {
                    int cur = kek[i * m + j];
                    ok = cur <= min(a[i], b[j]);
                    if (cur == a[i]) fa[i] = 1;
                    if (cur == b[j]) fb[j] = 1;
                }
            }
            ok &= count(ALL(fa), 1) == n && count(ALL(fb), 1) == m;
            ans += ok;
        } while (next_permutation(ALL(kek)));
        return ans;
    }

    void presolve() {
        int n, m;
        cin >> n >> m;
        vl a(n), b(m);
        fori (i, n) cin >> a[i];
        fori (i, m) cin >> b[i];
        cout << solve(a, b);
    }
}

namespace solution {

    const int N = 1e6 + 100;
    ll fact[N];
    ll inv[N];
    const ll M = 1e9 + 7;

    inline ll pw(ll a, ll p) {
        ll ret = 1;
        while (p) {
            if (p & 1) ret = (ret * a) % M;
            a = (a * a) % M;
            p >>= 1;
        }
        return ret;
    }

    inline ll nCr(int n, int k) {
        if (n < k) return 0;
        return fact[n] * inv[k] % M * inv[n - k] % M;
    }

    void init() {

        fact[0] = inv[0] = 1;
        for (int i = 1; i < N; ++i) {
            fact[i] = fact[i - 1] * i % M;
            inv[i] = pw(fact[i], M - 2);
        }


    }


    ll solve(vl a, vl b) {
        int n = a.size(), m = b.size();
        sort(ALL(a)), sort(ALL(b));
        a.resize(unique(ALL(a)) - a.begin());
        b.resize(unique(ALL(b)) - b.begin());
        if (a.size() != n || b.size() != m) return 0;
        vector<char> useda(n, 0), usedb(m, 0);
        vector<triple(int)> sorted;

        vector<vector<char>> noneed(n, vector<char> (m, 0));
        fori (i, n) {
            fori (j, m) {
                if (a[i] == b[j]) {
                    useda[i] = 1;
                    usedb[j] = 1;
                    noneed[i][j] = 1;
                }
                sorted.push_back(make_tuple(min(a[i], b[j]),  i, j));
            }
        }

        vector<vector<int>> calc(n, vector<int> (m , 0));

        sort(ALL(sorted));
        fori (i, sorted.size()) {
            int ii, jj, val;
            tie(val, ii, jj) = sorted[i];
            calc[ii][jj] = val - i;
        }

//        watch(calc);

        ll ans = 1;
        for (int i = 0; i < n; ++i) if (!useda[i]) {
                ll f = 1, s = 1;
                for (int j = 0; j < m; ++j) if (b[j] >= a[i]) {
                        ll nn = calc[i][j];
                        f = f * nn % M;
                        s = s * (nn - 1) % M;
                        noneed[i][j] = 1;
                    }
                ans = ans * (f - s + M) % M;
            }
        for (int j = 0; j < m; ++j) if (!usedb[j]){
                ll f = 1, s = 1;
                for (int i = 0; i < n; ++i) if (a[i] > b[j]) {
                        ll nn = calc[i][j];
                        f = f * nn % M;
                        s = s * (nn - 1) % M;
                        noneed[i][j] = 1;
                    }
                ans = ans * (f - s + M) % M;
            }
//        fori (i, n) {
//            fori (j, m) cout << bool(noneed[i][j]) << " ";
//            cout << endl;
//        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) if (!noneed[i][j]) {
                    ll mn = min(a[i], b[j]);
                    ll cur;
                    if (mn == a[i] && useda[i] || mn == b[i] && usedb[i]) {
                        cur = calc[i][j];
                    } else {
                        cur = calc[i][j];
                    }
                    ans = ans * cur % M;
                }
        }
        return ans;
    }


    void presolve() {
        int n, m;
        cin >> n >> m;
        vl a(n), b(m);
        fori (i, n) cin >> a[i];
        fori (i, m) cin >> b[i];
        cout << solve(a, b);
    }
}

void smain() {
    solution::init();
    solution::presolve();

}

Measured memory_bytes: 33000