N, M = [int(x) for x in input().split()]

A = [int(x) for x in input().split()]

B = [int(x) for x in input().split()]



ret = 1

MOD = 10 ** 9 + 7



from collections import Counter

import sys



ca = Counter(A)

cb = Counter(B)



if ca.most_common(1)[0][1] >= 2 or cb.most_common(1)[0][1] >= 2:

    print((0))

    sys.exit(0)



A.sort()

B.sort()



import bisect





def index(a, x):

    'Locate the leftmost value exactly equal to x'

    i = bisect.bisect_left(a, x)

    if i != len(a) and a[i] == x:

        return i

    return -1





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

    a = index(A, n)

    b = index(B, n)

    cntA = len(A) - bisect.bisect_right(A, n)

    cntB = len(B) - bisect.bisect_right(B, n)



    # 両方みつけた

    if a != -1 and b != -1:

        pass



    # Bの方だけ見つけた

    elif a == -1 and b != -1:

        ret *= cntA



    # Aの方だけ見つけた

    elif a != -1 and b == -1:

        ret *= cntB



    # 両方なかった

    else:

        ret *= (cntA * cntB - (N * M - n))

    ret = ret % MOD



print(ret)

N, M = [int(x) for x in input().split()]

A = [int(x) for x in input().split()]

B = [int(x) for x in input().split()]



ret = 1

MOD = 10 ** 9 + 7



A.sort(reverse=True)

B.sort(reverse=True)



aidx = bidx = 0



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



    # 両方みつけた

    if aidx < N and bidx < M and n == A[aidx] and n == B[bidx] and A[aidx] == B[bidx]:

        aidx += 1

        bidx += 1



    # Bの方だけ見つけた

    elif bidx < M and n == B[bidx]:

        ret *= aidx

        bidx += 1



    # Aの方だけ見つけた

    elif aidx < N and n == A[aidx]:

        ret *= bidx

        aidx += 1



    # 両方なかった

    else:

        ret *= (aidx * bidx - (N * M - n))

    ret = ret % MOD



print(ret)

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
ret = 1
MOD = 10**9 + 7
from collections import Counter
import sys

ca = Counter(A)
cb = Counter(B)
if ca.most_common(1)[0][1] >= 2 or cb.most_common(1)[0][1] >= 2:
    print((0))
    sys.exit(0)
A.sort()
B.sort()
import bisect


def index(a, x):
    "Locate the leftmost value exactly equal to x"
    i = bisect.bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return -1


for n in range(N * M, 0, -1):
    a = index(A, n)
    b = index(B, n)
    cntA = len(A) - bisect.bisect_right(A, n)
    cntB = len(B) - bisect.bisect_right(B, n)
    # 両方みつけた
    if a != -1 and b != -1:
        pass
    # Bの方だけ見つけた
    elif a == -1 and b != -1:
        ret *= cntA
    # Aの方だけ見つけた
    elif a != -1 and b == -1:
        ret *= cntB
    # 両方なかった
    else:
        ret *= cntA * cntB - (N * M - n)
    ret = ret % MOD
print(ret)

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
ret = 1
MOD = 10**9 + 7
A.sort(reverse=True)
B.sort(reverse=True)
aidx = bidx = 0
for n in range(N * M, 0, -1):
    # 両方みつけた
    if aidx < N and bidx < M and n == A[aidx] and n == B[bidx] and A[aidx] == B[bidx]:
        aidx += 1
        bidx += 1
    # Bの方だけ見つけた
    elif bidx < M and n == B[bidx]:
        ret *= aidx
        bidx += 1
    # Aの方だけ見つけた
    elif aidx < N and n == A[aidx]:
        ret *= bidx
        aidx += 1
    # 両方なかった
    else:
        ret *= aidx * bidx - (N * M - n)
    ret = ret % MOD
print(ret)

-from collections import Counter -import sys - -ca = Counter(A) -cb = Counter(B) -if ca.most_common(1)[0][1] >= 2 or cb.most_common(1)[0][1] >= 2: -    print((0)) -    sys.exit(0) -A.sort() -B.sort() -import bisect - - -def index(a, x): -    "Locate the leftmost value exactly equal to x" -    i = bisect.bisect_left(a, x) -    if i != len(a) and a[i] == x: -        return i -    return -1 - - +A.sort(reverse=True) +B.sort(reverse=True) +aidx = bidx = 0 -    a = index(A, n) -    b = index(B, n) -    cntA = len(A) - bisect.bisect_right(A, n) -    cntB = len(B) - bisect.bisect_right(B, n) -    if a != -1 and b != -1: -        pass +    if aidx < N and bidx < M and n == A[aidx] and n == B[bidx] and A[aidx] == B[bidx]: +        aidx += 1 +        bidx += 1 -    elif a == -1 and b != -1: -        ret *= cntA +    elif bidx < M and n == B[bidx]: +        ret *= aidx +        bidx += 1 -    elif a != -1 and b == -1: -        ret *= cntB +    elif aidx < N and n == A[aidx]: +        ret *= bidx +        aidx += 1 -        ret *= cntA * cntB - (N * M - n) +        ret *= aidx * bidx - (N * M - n)