Buggy python submission (verdict: Runtime Error):
from bisect import bisect_right
p = 10**9+7
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
if CA[N*M]!=1:flag=1
if CB[N*M]!=1:flag=1
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Fixed submission:
from bisect import bisect_right
p = 10**9+7
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
if N*M not in CA or CA[N*M]!=1:flag=1
if N*M not in CB or CB[N*M]!=1:flag=1
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Buggy python submission (verdict: Runtime Error):
from bisect import bisect_right
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Fixed submission:
from bisect import bisect_right
p = 10**9+7
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
if N*M not in CA or CA[N*M]!=1:flag=1
if N*M not in CB or CB[N*M]!=1:flag=1
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Buggy python submission (verdict: Runtime Error):
from bisect import bisect_right
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Fixed submission:
from bisect import bisect_right
p = 10**9+7
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
if N*M not in CA or CA[N*M]!=1:flag=1
if N*M not in CB or CB[N*M]!=1:flag=1
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
flag = 0
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = 0
            for j in range(M):
                if B[j]>k:
                    cnt += 1
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = 0
            for i in range(N):
                if A[i]>k:
                    cnt += 1
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = 0
            for i in range(N):
                if A[i]>k:
                    cnt1 += 1
            cnt2 = 0
            for j in range(M):
                if B[j]>k:
                    cnt2 += 1
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Fixed submission:
from bisect import bisect_right
p = 10**9+7
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
if N*M not in CA or CA[N*M]!=1:flag=1
if N*M not in CB or CB[N*M]!=1:flag=1
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
B = list(map(int,input().split()))
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
dp = [0 for _ in range(N*M+1)]
dp[N*M] = 1
for k in range(N*M-1,0,-1):
    if indA[k]==1 and indB[k]==1:
        dp[k] = dp[k+1]
    elif indA[k]==1 and indB[k]==0:
        cnt = 0
        for j in range(M):
            if B[j]>k:
                cnt += 1
        dp[k] = (dp[k+1]*cnt)%p
    elif indA[k]==0 and indB[k]==1:
        cnt = 0
        for i in range(N):
            if A[i]>k:
                cnt += 1
        dp[k] = (dp[k+1]*cnt)%p
    else:
        cnt1 = 0
        for i in range(N):
            if A[i]>k:
                cnt1 += 1
        cnt2 = 0
        for j in range(M):
            if B[j]>k:
                cnt2 += 1
        cnt = cnt1*cnt2-(N*M-k)
        dp[k] = (dp[k+1]*cnt)%p
print(dp[1])

Fixed submission:
from bisect import bisect_right
p = 10**9+7
N,M = map(int,input().split())
A = list(map(int,input().split()))
CA = {}
for i in range(N):
    if A[i] not in CA:
        CA[A[i]] = 0
    CA[A[i]] += 1
indA = [0 for _ in range(N*M+1)]
for i in range(N):
    indA[A[i]] = 1
A1 = sorted(A[:])
B = list(map(int,input().split()))
CB = {}
for j in range(M):
    if B[j] not in CB:
        CB[B[j]] = 0
    CB[B[j]] += 1
indB = [0 for _ in range(N*M+1)]
for j in range(M):
    indB[B[j]] = 1
B1 = sorted(B[:])
flag = 0
if N*M not in CA or CA[N*M]!=1:flag=1
if N*M not in CB or CB[N*M]!=1:flag=1
for a in CA:
    if CA[a]>1:
        flag = 1
        break
for b in CB:
    if CB[b]>1:
        flag = 1
        break
if flag==1:
    print(0)
else:
    dp = [0 for _ in range(N*M+1)]
    dp[N*M] = 1
    for k in range(N*M-1,0,-1):
        if indA[k]==1 and indB[k]==1:
            dp[k] = dp[k+1]
        elif indA[k]==1 and indB[k]==0:
            cnt = bisect_right(B1,k)
            cnt = M-cnt
            dp[k] = (dp[k+1]*cnt)%p
        elif indA[k]==0 and indB[k]==1:
            cnt = bisect_right(A1,k)
            cnt = N-cnt
            dp[k] = (dp[k+1]*cnt)%p
        else:
            cnt1 = bisect_right(A1,k)
            cnt1 = N-cnt1
            cnt2 = bisect_right(B1,k)
            cnt2 = M-cnt2
            cnt = cnt1*cnt2-(N*M-k)
            dp[k] = (dp[k+1]*cnt)%p
    print(dp[1])