n,m=list(map(int,input().split()));a,b=[set(map(int,input().split())) for j in range(2)];c=1;N=M=0

for k in range(m*n,0,-1):

 A=k in a;B=k in b

 if A and B:M+=1;N+=1

 elif A:c*=M;N+=1

 elif B:c*=N;M+=1

 else:c*=M*N-m*n+k

 c%=10**9+7

print(c)
eval(input());a,b=[set(map(int,input().split())) for j in[0]*2];c=1;N=M=0;x=max(a)

for k in range(x,0,-1):

 A=k in a;B=k in b

 if A and B:M+=1;N+=1

 elif A:c*=M;N+=1

 elif B:c*=N;M+=1

 else:c*=M*N-x+k

 c%=10**9+7

print(c)
n, m = list(map(int, input().split()))
a, b = [set(map(int, input().split())) for j in range(2)]
c = 1
N = M = 0
for k in range(m * n, 0, -1):
    A = k in a
    B = k in b
    if A and B:
        M += 1
        N += 1
    elif A:
        c *= M
        N += 1
    elif B:
        c *= N
        M += 1
    else:
        c *= M * N - m * n + k
    c %= 10**9 + 7
print(c)

eval(input())
a, b = [set(map(int, input().split())) for j in [0] * 2]
c = 1
N = M = 0
x = max(a)
for k in range(x, 0, -1):
    A = k in a
    B = k in b
    if A and B:
        M += 1
        N += 1
    elif A:
        c *= M
        N += 1
    elif B:
        c *= N
        M += 1
    else:
        c *= M * N - x + k
    c %= 10**9 + 7
print(c)

-n, m = list(map(int, input().split())) -a, b = [set(map(int, input().split())) for j in range(2)] +eval(input()) +a, b = [set(map(int, input().split())) for j in [0] * 2] -for k in range(m * n, 0, -1): +x = max(a) +for k in range(x, 0, -1): -        c *= M * N - m * n + k +        c *= M * N - x + k