import bisect

N,M = list(map(int,input().split()))

A = sorted(list(map(int,input().split())))

B = sorted(list(map(int,input().split())))

sa = set(A)

sb = set(B)

MOD = 10**9+7



if len(sa) != N or len(sb) != M:

    print((0))

    exit()



ans = 1

for n in range(N*M, 0, -1):

    if n in sa and n in sb: continue

    if n in sa:

        vacant = M - bisect.bisect(B,n)

    elif n in sb:

        vacant = N - bisect.bisect(A,n)

    else:

        i = N - bisect.bisect(A,n)

        j = M - bisect.bisect(B,n)

        vacant = i*j - (N*M-n)

        if vacant <= 0:

            print((0))

            exit()

    ans *= vacant

    ans %= MOD



print(ans)
N,M = list(map(int,input().split()))

A = list(map(int,input().split()))

B = list(map(int,input().split()))

A.sort()

B.sort()

sa = set(A)

sb = set(B)

if len(sa)!=N or len(sb)!=M:

    print((0))

    exit()

MOD = 10**9+7



ans = 1

w = h = 0

for v in range(N*M,0,-1):

    if v in sa and v in sb:

        w += 1

        h += 1

    elif v in sa:

        ans *= w

        ans %= MOD

        h += 1

    elif v in sb:

        ans *= h

        ans %= MOD

        w += 1

    else:

        free = w*h - (N*M - v)

        if free <= 0:

            print((0))

            exit()

        ans *= free

        ans %= MOD

print(ans)
import bisect

N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
sa = set(A)
sb = set(B)
MOD = 10**9 + 7
if len(sa) != N or len(sb) != M:
    print((0))
    exit()
ans = 1
for n in range(N * M, 0, -1):
    if n in sa and n in sb:
        continue
    if n in sa:
        vacant = M - bisect.bisect(B, n)
    elif n in sb:
        vacant = N - bisect.bisect(A, n)
    else:
        i = N - bisect.bisect(A, n)
        j = M - bisect.bisect(B, n)
        vacant = i * j - (N * M - n)
        if vacant <= 0:
            print((0))
            exit()
    ans *= vacant
    ans %= MOD
print(ans)

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.sort()
B.sort()
sa = set(A)
sb = set(B)
if len(sa) != N or len(sb) != M:
    print((0))
    exit()
MOD = 10**9 + 7
ans = 1
w = h = 0
for v in range(N * M, 0, -1):
    if v in sa and v in sb:
        w += 1
        h += 1
    elif v in sa:
        ans *= w
        ans %= MOD
        h += 1
    elif v in sb:
        ans *= h
        ans %= MOD
        w += 1
    else:
        free = w * h - (N * M - v)
        if free <= 0:
            print((0))
            exit()
        ans *= free
        ans %= MOD
print(ans)

-import bisect - -A = sorted(list(map(int, input().split()))) -B = sorted(list(map(int, input().split()))) +A = list(map(int, input().split())) +B = list(map(int, input().split())) +A.sort() +B.sort() -MOD = 10**9 + 7 +MOD = 10**9 + 7 -for n in range(N * M, 0, -1): -    if n in sa and n in sb: -        continue -    if n in sa: -        vacant = M - bisect.bisect(B, n) -    elif n in sb: -        vacant = N - bisect.bisect(A, n) +w = h = 0 +for v in range(N * M, 0, -1): +    if v in sa and v in sb: +        w += 1 +        h += 1 +    elif v in sa: +        ans *= w +        ans %= MOD +        h += 1 +    elif v in sb: +        ans *= h +        ans %= MOD +        w += 1 -        i = N - bisect.bisect(A, n) -        j = M - bisect.bisect(B, n) -        vacant = i * j - (N * M - n) -        if vacant <= 0: +        free = w * h - (N * M - v) +        if free <= 0: -    ans *= vacant -    ans %= MOD +        ans *= free +        ans %= MOD