P=10**9+7

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

X=sorted([int(i) for i in input().split()])

Y=sorted([int(i) for i in input().split()])

A=[i for i in X]

B=[i for i in Y]

if len(set(A))!=N or len(set(B))!=M:

    print((0))

    exit()

L=[]

x=0

y=0

D=N*M

while(len(A)>0 and len(B)>0):

    if max(A[-1],B[-1])==D:

        if A[-1]>B[-1]:

            A.pop()

            L.append(y)

            x+=1

        elif A[-1]<B[-1]:

            B.pop()

            L.append(x)

            y+=1

        else:

            A.pop()

            B.pop()

            x+=1

            y+=1

        D-=1

    else:

        C=max(A[-1],B[-1])+1

        tmp=x*y-(N*M-D)

        for i in range(C,D+1)[::-1]:

            L.append(tmp)

            tmp-=1

        D=max(A[-1],B[-1])

if len(A)>0:

    D=min(Y)-1

    while(len(A)>0):

        C=A[-1]+1

        tmp=x*y-(N*M-D)

        for i in range(C,D+1)[::-1]:

            L.append(tmp)

            tmp-=1

        D=A.pop()-1

        x+=1

        L.append(M)

if len(B)>0:

    D=min(X)-1

    while(len(B)>0):

        C=B[-1]+1

        tmp=x*y-(N*M-D)

        for i in range(C,D+1)[::-1]:

            L.append(tmp)

            tmp-=1

        D=B.pop()-1

        y+=1

        L.append(N)

m=min(min(X),min(Y))-1

for i in range(1,m+1):

    L.append(i)

ans=1

for i in L:

    ans*=i

    ans%=P

print(ans)

P=10**9+7

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

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

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

mx=A[0]-1

my=B[0]-1

m=min(mx,my)

ans=1

if len(set(A))!=N or len(set(B))!=M:

    print((0))

    exit()

x=0

y=0

D=N*M

while(len(A)>0 and len(B)>0):

    if max(A[-1],B[-1])==D:

        if A[-1]>B[-1]:

            A.pop()

            ans*=y

            ans%=P

            x+=1

        elif A[-1]<B[-1]:

            B.pop()

            ans*=x

            ans%=P

            y+=1

        else:

            A.pop()

            B.pop()

            x+=1

            y+=1

        D-=1

    else:

        C=max(A[-1],B[-1])+1

        for i in range(C,D+1):

            ans*=x*y-N*M+i

            ans%=P

        D=max(A[-1],B[-1])

if len(A)>0:

    D=my

    while(len(A)>0):

        C=A[-1]+1

        for i in range(C,D+1)[::-1]:

            ans*=x*y-N*M+i

            ans%=P

        D=A.pop()-1

        x+=1

        ans*=M

        ans%=P

if len(B)>0:

    D=mx

    while(len(B)>0):

        C=B[-1]+1

        for i in range(C,D+1):

            ans*=x*y-N*M+i

            ans%=P

        D=B.pop()-1

        y+=1

        ans*=N

        ans%=P

for i in range(1,m+1):

    ans*=i

    ans%=P

print(ans)

P = 10**9 + 7
N, M = list(map(int, input().split()))
X = sorted([int(i) for i in input().split()])
Y = sorted([int(i) for i in input().split()])
A = [i for i in X]
B = [i for i in Y]
if len(set(A)) != N or len(set(B)) != M:
    print((0))
    exit()
L = []
x = 0
y = 0
D = N * M
while len(A) > 0 and len(B) > 0:
    if max(A[-1], B[-1]) == D:
        if A[-1] > B[-1]:
            A.pop()
            L.append(y)
            x += 1
        elif A[-1] < B[-1]:
            B.pop()
            L.append(x)
            y += 1
        else:
            A.pop()
            B.pop()
            x += 1
            y += 1
        D -= 1
    else:
        C = max(A[-1], B[-1]) + 1
        tmp = x * y - (N * M - D)
        for i in range(C, D + 1)[::-1]:
            L.append(tmp)
            tmp -= 1
        D = max(A[-1], B[-1])
if len(A) > 0:
    D = min(Y) - 1
    while len(A) > 0:
        C = A[-1] + 1
        tmp = x * y - (N * M - D)
        for i in range(C, D + 1)[::-1]:
            L.append(tmp)
            tmp -= 1
        D = A.pop() - 1
        x += 1
        L.append(M)
if len(B) > 0:
    D = min(X) - 1
    while len(B) > 0:
        C = B[-1] + 1
        tmp = x * y - (N * M - D)
        for i in range(C, D + 1)[::-1]:
            L.append(tmp)
            tmp -= 1
        D = B.pop() - 1
        y += 1
        L.append(N)
m = min(min(X), min(Y)) - 1
for i in range(1, m + 1):
    L.append(i)
ans = 1
for i in L:
    ans *= i
    ans %= P
print(ans)

P = 10**9 + 7
N, M = list(map(int, input().split()))
A = sorted([int(i) for i in input().split()])
B = sorted([int(i) for i in input().split()])
mx = A[0] - 1
my = B[0] - 1
m = min(mx, my)
ans = 1
if len(set(A)) != N or len(set(B)) != M:
    print((0))
    exit()
x = 0
y = 0
D = N * M
while len(A) > 0 and len(B) > 0:
    if max(A[-1], B[-1]) == D:
        if A[-1] > B[-1]:
            A.pop()
            ans *= y
            ans %= P
            x += 1
        elif A[-1] < B[-1]:
            B.pop()
            ans *= x
            ans %= P
            y += 1
        else:
            A.pop()
            B.pop()
            x += 1
            y += 1
        D -= 1
    else:
        C = max(A[-1], B[-1]) + 1
        for i in range(C, D + 1):
            ans *= x * y - N * M + i
            ans %= P
        D = max(A[-1], B[-1])
if len(A) > 0:
    D = my
    while len(A) > 0:
        C = A[-1] + 1
        for i in range(C, D + 1)[::-1]:
            ans *= x * y - N * M + i
            ans %= P
        D = A.pop() - 1
        x += 1
        ans *= M
        ans %= P
if len(B) > 0:
    D = mx
    while len(B) > 0:
        C = B[-1] + 1
        for i in range(C, D + 1):
            ans *= x * y - N * M + i
            ans %= P
        D = B.pop() - 1
        y += 1
        ans *= N
        ans %= P
for i in range(1, m + 1):
    ans *= i
    ans %= P
print(ans)

-X = sorted([int(i) for i in input().split()]) -Y = sorted([int(i) for i in input().split()]) -A = [i for i in X] -B = [i for i in Y] +A = sorted([int(i) for i in input().split()]) +B = sorted([int(i) for i in input().split()]) +mx = A[0] - 1 +my = B[0] - 1 +m = min(mx, my) +ans = 1 -L = [] -            L.append(y) +            ans *= y +            ans %= P -            L.append(x) +            ans *= x +            ans %= P -        tmp = x * y - (N * M - D) -        for i in range(C, D + 1)[::-1]: -            L.append(tmp) -            tmp -= 1 +        for i in range(C, D + 1): +            ans *= x * y - N * M + i +            ans %= P -    D = min(Y) - 1 +    D = my -        tmp = x * y - (N * M - D) -            L.append(tmp) -            tmp -= 1 +            ans *= x * y - N * M + i +            ans %= P -        L.append(M) +        ans *= M +        ans %= P -    D = min(X) - 1 +    D = mx -        tmp = x * y - (N * M - D) -        for i in range(C, D + 1)[::-1]: -            L.append(tmp) -            tmp -= 1 +        for i in range(C, D + 1): +            ans *= x * y - N * M + i +            ans %= P -        L.append(N) -m = min(min(X), min(Y)) - 1 +        ans *= N +        ans %= P -    L.append(i) -ans = 1 -for i in L: