Buggy python submission (verdict: Time Limit Exceeded):
#https://atcoder.jp/contests/keyence2019/tasks/keyence2019_d
#2019-01-13
import sys
P = 10**9 + 7
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.sort(reverse = 1)
B.sort(reverse = 1)

if A[0] != N*M or B[0] != N*M:
    print(0)
    sys.exit()
for i in range(N-1):
    if A[i] == A[i+1]: 
        print(0)
        sys.exit()
for i in range(M-1):
    if B[i] == B[i+1]:
        print(0)
        sys.exit()

ans = 1
used_tate = []
used_yoko = []
oita = 0
#大きい数字から順番に、おける場所が制限されている。
for number in range(N*M, 0, -1):
    if number in A and number in B:
        used_tate += [1]
        used_yoko += [1]
        oita += 1
    elif number in A and number not in B:
        used_tate += [1]
        ans *= len(used_yoko)
        oita += 1
    elif number not in A and number in B:
        used_yoko += [1]
        ans *= len(used_tate)
        oita += 1
    else:
        ans *= len(used_tate)*len(used_yoko) -oita
        oita += 1 
    ans = ans%P
print(ans)


Fixed submission:
#https://atcoder.jp/contests/keyence2019/tasks/keyence2019_d
#2019-01-13
import sys 
from collections import deque
P = 10**9 + 7
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.sort(reverse = 1)
B.sort(reverse = 1)
A = deque(A)
B = deque(B)

if A[0] != N*M or B[0] != N*M:
    print(0)
    sys.exit()
for i in range(N-1):
    if A[i] == A[i+1]: 
        print(0)
        sys.exit()
for i in range(M-1):
    if B[i] == B[i+1]:
        print(0)
        sys.exit()

ans = 1
used_tate = []
used_yoko = []
oita = 0

p = A.popleft()
q = B.popleft()
#大きい数字から順番に、おける場所が制限されている。
for number in range(N*M, 0, -1):
    if number == p and number == q:
        used_tate += [1]
        used_yoko += [1]
        oita += 1
        try:
            p = A.popleft()
        except:
            p = 0
        try:
            q = B.popleft()
        except:
            q = 0
    elif number == p and number != q:
        used_tate += [1]
        ans *= len(used_yoko)
        oita += 1
        try:
            p = A.popleft()
        except:
            p = 0
    elif number != p and number == q:
        used_yoko += [1]
        ans *= len(used_tate)
        oita += 1
        try:
            q = B.popleft()
        except:
            q = 0
    else:
        ans *= len(used_tate)*len(used_yoko) - oita
        oita += 1
    ans = ans%P
print(ans)

Buggy python submission (verdict: Runtime Error):
#https://atcoder.jp/contests/keyence2019/tasks/keyence2019_d
#2019-01-13
import sys
P = 10**9 + 7
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.sort(reverse = 1)
B.sort(reverse = 1)

if A[0] != N*M or B[0] != N*M:
    print(0)
    sys.exit()
for i in range(N-1):
    if A[i] == A[i+1]: 
        print(0)
        sys.exit()
for i in range(M-1):
    if B[i] == B[i+1]:
        print(0)
        sys.exit()

#大きい数字から順番に、おける場所が制限されている。
0/0

Fixed submission:
#https://atcoder.jp/contests/keyence2019/tasks/keyence2019_d
#2019-01-13
import sys 
from collections import deque
P = 10**9 + 7
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.sort(reverse = 1)
B.sort(reverse = 1)
A = deque(A)
B = deque(B)

if A[0] != N*M or B[0] != N*M:
    print(0)
    sys.exit()
for i in range(N-1):
    if A[i] == A[i+1]: 
        print(0)
        sys.exit()
for i in range(M-1):
    if B[i] == B[i+1]:
        print(0)
        sys.exit()

ans = 1
used_tate = []
used_yoko = []
oita = 0

p = A.popleft()
q = B.popleft()
#大きい数字から順番に、おける場所が制限されている。
for number in range(N*M, 0, -1):
    if number == p and number == q:
        used_tate += [1]
        used_yoko += [1]
        oita += 1
        try:
            p = A.popleft()
        except:
            p = 0
        try:
            q = B.popleft()
        except:
            q = 0
    elif number == p and number != q:
        used_tate += [1]
        ans *= len(used_yoko)
        oita += 1
        try:
            p = A.popleft()
        except:
            p = 0
    elif number != p and number == q:
        used_yoko += [1]
        ans *= len(used_tate)
        oita += 1
        try:
            q = B.popleft()
        except:
            q = 0
    else:
        ans *= len(used_tate)*len(used_yoko) - oita
        oita += 1
    ans = ans%P
print(ans)