#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdint.h>
#include <string.h>

#include <unordered_set>

//#define _USE_MATH_DEFINES
//#include <math.h>


#include <vector>
#include <list>

#include <set>
#include <map>

#include <unordered_map>

#include <queue>

#include <string>

#include <vector>

#define sqr(x) (x) * (x)

#include <algorithm>
#include <functional>

#include <bitset>

#include <functional>

#include <chrono>

typedef unsigned int u32;
typedef int i32;

typedef unsigned long long int u64;
typedef long long int i64;

typedef uint16_t u16;
typedef int16_t i16;

typedef uint8_t u8;
typedef int8_t i8;


using namespace std;
using namespace std::chrono;

struct less_key
{
    bool operator() (pair<i64, i64> p1, pair<i64, i64> p2)
    {
        return p1.first < p2.first || (p1.first == p2.first && p1.second < p2.second);
    }
};

struct less_key2
{
    bool operator() (pair<i64, i64> p1, pair<i64, i64> p2)
    {
        return p1.second > p2.second || (p1.second == p2.second && p1.first > p2.first);
    }
};

//
//struct pair_hash
//{
//    std::size_t operator()(const i64& k) const
//    {
//        return static_cast<size_t>(k.first ^ k.second);
//    }
//};

#include <random>

const i64 mod = 1000000007ll;
const i64 mod2 = 998244353ll;
const i64 inf = 10000000000000007ll;

const double eps = 1e-10;

template <typename T = i64, T D = 0>
class MaxOp {
public:
    static T op(const T& a, const T&b) {
        return max(a, b);
    }

    static T def() {
        return (T)D;
    }
};

template <typename T = i64, T D = inf>
class MinOp {
public:
    static T op(const T& a, const T&b) {
        return min(a, b);
    }

    static T def() {
        return (T)D;
    }
};

template <typename T = i64, T D = 0>
class SumOp {
public:
    static T op(const T& a, const T&b) {
        return a + b;
    }

    static T def() {
        return (T)D;
    }
};

template <typename D, typename T = i64>
class SegTree {
public:
    i64 n;
    vector<T> t;

private:
    void build(const i64* a, int v, int tl, int tr) {
        if (tl == tr)
            t[v] = a[tl];
        else {
            int tm = (tl + tr) / 2;
            build(a, v * 2, tl, tm);
            build(a, v * 2 + 1, tm + 1, tr);
            t[v] = D().op(t[v * 2], t[v * 2 + 1]);
        }
    }

    i64 get(int v, int tl, int tr, int l, int r) {
        if (l > r) {
            return D().def();
        }
        if (l == tl && r == tr) {
            return t[v];
        }
        int tm = (tl + tr) / 2;
        return D().op(get(v * 2, tl, tm, l, min(r, tm)),
            get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
    }

    void update(int v, int tl, int tr, int pos, int new_val) {
        if (tl == tr) {
            t[v] = new_val;
        }
        else {
            int tm = (tl + tr) / 2;
            if (pos <= tm) {
                update(v * 2, tl, tm, pos, new_val);
            }
            else {
                update(v * 2 + 1, tm + 1, tr, pos, new_val);
            }
            t[v] = D().op(t[v * 2] + t[v * 2 + 1]);
        }
    }

public:
    void build(const vector<i64 >& a) {
        n = a.size();
        t.resize(n * 4);
        build(a.data(), 1, 0, n - 1);
    }

    void build(i64* a, i64 n) {
        this->n = n;
        t.resize(n * 4);
        build(a, 1, 0, n - 1);
    }

    i64 get(i64 l, i64 r) {
        return get(1, 0, n - 1, l, r);
    }

    void update(i64 pos, i64 new_val) {
        update(1, 0, n - 1, pos, new_val);
    }
};


// delimeter

i64 n, m;


i64 a[1005];
i64 b[1005];

pair<pair<i64, i64>, pair<i64, i64>> z[1005 * 1005];

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed);

    cin >> n >> m;

    for (i64 i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (i64 i = 1; i <= m; i++) {
        cin >> b[i];
    }

    sort(a + 1, a + n + 1, std::greater<i64>());
    sort(b + 1, b + m + 1, std::greater<i64>());

    i64 i = 1;
    i64 j = 1;

    const pair<pair<i64, i64>, pair<i64, i64>> zp = { { 0ll,0ll },{ 0ll,0ll } };

    for (;;) {
        if (i > n && j > m) {
            break;
        }
        if (a[i] == b[j]) {
            if (z[a[i]] != zp) {
                cout << 0 << endl;
                return 0;
            }
            z[a[i]] = { {i, j}, {i, j} };
            i++;
            j++;
        }
        else if (a[i] > b[j]) {
            if (z[a[i]] != zp) {
                cout << 0 << endl;
                return 0;
            }
            z[a[i]] = { {i, 1}, {i, j - 1} };
            i++;
        }
        else if (a[i] < b[j]) {
            if (z[b[j]] != zp) {
                cout << 0 << endl;
                return 0;
            }
            z[b[j]] = { {1, j}, {i - 1, j} };
            j++;
        }
    }

    if (i != n + 1 || j != m + 1) {
        cout << 0 << endl;
        return 0;
    }

    i = 0;
    j = 0;
    i64 R = 1;
    for (i64 k = m * n; k > 0; k--) {
        if (z[k] != zp) {
            i64 t1 = (z[k].second.first -  z[k].first.first + 1);
            i64 t2 = (z[k].second.second - z[k].first.second + 1);
            if (t1 <= 0 || t2 <= 0) {
                cout << 0 << endl;
                return 0;
            }
            R *= t1 * t2;
            R %= mod;
        }
        else {
            for (; i < n; i++) {
                if (a[i + 1] < k) {
                    break;
                }
            }
            for (; j < m; j++) {
                if (b[j + 1] < k) {
                    break;
                }
            }

            i64 t = i * j - (m * n - k);
            if (t <= 0) {
                cout << 0 << endl;
                return 0;
            }
            R *= t;
            R %= mod;
        }
    }

    cout << R << endl;
    return 0;
}

Measured memory_bytes: 4352