#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
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; }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　  |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/










int H, W, A[1010], B[1010];
vector<int> PY[1010101], PX[1010101];
int EX[1010], EY[1010];
int EXN = 0, EYN = 0;
//---------------------------------------------------------------------------------------------------
mint solve() {
    mint ans = 1;
    int done = 0;
    rrep(i, H*W, 1) {
        int npy = PY[i].size();
        int npx = PX[i].size();

        if (npy == 0) {
            if (npx == 0) {
                // 無い
                int can = EXN * EYN - done;
                if (can <= 0) return 0;
                ans *= can;
                done++;
            }
            else if (npx == 1) {
                // x座標側だけある
                if (EYN == 0) return 0;
                int x = PX[i][0];
                EX[x] = i;
                EXN++;
                ans *= EYN;
                done++;
            }
            else return 0;
        }
        else if (npy == 1) {
            if (npx == 0) {
                // y座標側だけある
                if (EXN == 0) return 0;
                int y = PY[i][0];
                EY[y] = i;
                EYN++;
                ans *= EXN;
                done++;
            }
            else if (npx == 1) {
                // どっちもある
                int x = PX[i][0];
                int y = PY[i][0];
                EX[x] = i;
                EXN++;
                EY[y] = i;
                EYN++;
                done++;
            }
            else return 0;
        }
        else return 0;
    }
    return ans;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> H >> W;
    rep(i, 0, H) cin >> A[i];
    rep(i, 0, W) cin >> B[i];
    
    rep(y, 0, H) PY[A[y]].push_back(y);
    rep(x, 0, W) PX[B[x]].push_back(x);

    cout << solve() << endl;
}


Measured memory_bytes: 47744