import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

int[] di = [-1, 0, 1, -1, 0, 1, -1, 0, 1];
int[] dj = [-1, -1, -1, 0, 0, 0, 1, 1, 1];
alias Block = Tuple!(int, "i", int, "j");

void main() {
    int n, m;
    scan(n, m);
    auto a = readArr!(int);
    auto b = readArr!(int);
    auto as = a.sort();
    auto bs = b.sort();

    if (as.length > as.uniq.count || bs.length > bs.uniq.count) {
        writeln(0);
        return;
    }

    long ans = 1;

    foreach (x ; 1 .. n * m + 1) {
        auto inA = x in as;
        auto inB = x in bs;

        auto bux = bs.upperBound(x).length.to!long;
        auto aux = as.upperBound(x).length.to!long;

        if (inA && inB) {

        }
        else if (inA) {
            ans *= bux;
            ans %= mod;
        }
        else if (inB) {
            ans *= aux;
            ans %= mod;
        }
        else {
            ans *= (aux * bux % mod - (n * m - x) + mod) % mod;
            ans %= mod;
        }

    }

    writeln(ans);
}





void yes(bool b) {writeln(b ? "Yes" : "No");}
void YES(bool b) {writeln(b ? "YES" : "NO");}
T[] readArr(T)() {return readln.split.to!(T[]);}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
