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

enum P = 10L^^9+7;

void main()
{
    auto nm = readln.split.to!(int[]);
    auto N = nm[0];
    auto M = nm[1];

    auto ab = new bool[][](2, N*M);
    foreach (a; readln.split.to!(int[])) {
        if (ab[0][a-1]) {
            writeln(0);
            return;
        }
        ab[0][a-1] = true;
    }
    foreach (b; readln.split.to!(int[])) {
        if (ab[1][b-1]) {
            writeln(0);
            return;
        }
        ab[1][b-1] = true;
    }

    long n, m, b, r = 1;
    foreach_reverse (i; 0..N*M) {
        if (ab[0][i] && ab[1][i]) {
            n += 1;
            m += 1;
        } else if (ab[0][i]) {
            n += 1;
            (r *= m) %= P;
        } else if (ab[1][i]) {
            m += 1;
            (r *= n) %= P;
        } else {
            (r *= (m*n-b)) %= P;
        }
        b += 1;
    }
    writeln(r);
}