n, m = list(map(int, input().split()))

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

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



mod = 10**9+7



A_ = set(A)

B_ = set(B)



if len(A) != len(A_) or len(B) != len(B_):

    print((0))

    exit()



A.sort()

B.sort()



import bisect



ans = 1

for i in reversed(list(range(1, n*m+1))):

    if i in A_ and i in B_:

        ans *= 1

    elif i in A_ and i not in B_:

        ans *= (len(B)-bisect.bisect_left(B, i))

    elif i not in A_ and i in B_:

        ans *= (len(A)-bisect.bisect_left(A, i))

    else:

        a = len(A)-bisect.bisect_left(A, i)

        b = len(B)-bisect.bisect_left(B, i)

        ans *= a*b-(len(A)*len(B)-i)

    ans %= mod

    

print(ans)
n, m = list(map(int, input().split()))

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

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



mod = 10**9+7



SA = set(A)

SB = set(B)

A.sort()

B.sort()



if len(SA) != len(A) or len(SB) != len(B):

    print((0))

    exit()



ans = 1

import bisect

for i in reversed(list(range(1, n*m+1))):

    if i in SA and i in SB:

        ans *= 1

    elif i in SA:

        j = bisect.bisect_left(B, i)

        ans *= m-j

    elif i in SB:

        j = bisect.bisect_left(A, i)

        ans *= n-j

    else:

        j = bisect.bisect_left(A, i)

        k = bisect.bisect_left(B, i)

        ans *= (n-j)*(m-k) - (n*m-i)

    ans %= mod

print(ans)
n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
mod = 10**9 + 7
A_ = set(A)
B_ = set(B)
if len(A) != len(A_) or len(B) != len(B_):
    print((0))
    exit()
A.sort()
B.sort()
import bisect

ans = 1
for i in reversed(list(range(1, n * m + 1))):
    if i in A_ and i in B_:
        ans *= 1
    elif i in A_ and i not in B_:
        ans *= len(B) - bisect.bisect_left(B, i)
    elif i not in A_ and i in B_:
        ans *= len(A) - bisect.bisect_left(A, i)
    else:
        a = len(A) - bisect.bisect_left(A, i)
        b = len(B) - bisect.bisect_left(B, i)
        ans *= a * b - (len(A) * len(B) - i)
    ans %= mod
print(ans)

n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
mod = 10**9 + 7
SA = set(A)
SB = set(B)
A.sort()
B.sort()
if len(SA) != len(A) or len(SB) != len(B):
    print((0))
    exit()
ans = 1
import bisect

for i in reversed(list(range(1, n * m + 1))):
    if i in SA and i in SB:
        ans *= 1
    elif i in SA:
        j = bisect.bisect_left(B, i)
        ans *= m - j
    elif i in SB:
        j = bisect.bisect_left(A, i)
        ans *= n - j
    else:
        j = bisect.bisect_left(A, i)
        k = bisect.bisect_left(B, i)
        ans *= (n - j) * (m - k) - (n * m - i)
    ans %= mod
print(ans)

-A_ = set(A) -B_ = set(B) -if len(A) != len(A_) or len(B) != len(B_): +SA = set(A) +SB = set(B) +A.sort() +B.sort() +if len(SA) != len(A) or len(SB) != len(B): -A.sort() -B.sort() +ans = 1 -ans = 1 -    if i in A_ and i in B_: +    if i in SA and i in SB: -    elif i in A_ and i not in B_: -        ans *= len(B) - bisect.bisect_left(B, i) -    elif i not in A_ and i in B_: -        ans *= len(A) - bisect.bisect_left(A, i) +    elif i in SA: +        j = bisect.bisect_left(B, i) +        ans *= m - j +    elif i in SB: +        j = bisect.bisect_left(A, i) +        ans *= n - j -        a = len(A) - bisect.bisect_left(A, i) -        b = len(B) - bisect.bisect_left(B, i) -        ans *= a * b - (len(A) * len(B) - i) +        j = bisect.bisect_left(A, i) +        k = bisect.bisect_left(B, i) +        ans *= (n - j) * (m - k) - (n * m - i)