Buggy python submission (verdict: Runtime Error):
mod = 10**9+7
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

a = sorted(a, reverse=True)
b = sorted(b, reverse=True)

c = [0 for _ in range(n*m)]
for i in range(n):
    c[a[i]-1] += 1
    if c[a[i]-1] == 2:
        print(0)
        exit()

c = [0 for _ in range(n*m)]
for i in range(m):
    c[b[i]-1] += 1
    if c[b[i]-1] == 2:
        print(0)
        exit()

ans = 1

i = 0
j = 0

while i < n and j < m:
    if n*m > max(a[i], b[j]) + i*j:
        print(0)
        exit()
    if a[i] == b[i]:
        i += 1
        j += 1
    elif a[i] > b[j]:
        ans *= j
        ans %= mod
        i += 1
    else:
        ans *= i
        ans %= mod
        j += 1

res = n*m - min(n, m)

for i in range(1, res+1):
    ans *= i
    ans %= mod

print(ans)


Fixed submission:
mod = 10**9+7
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

aa = [0 for _ in range(n*m)]
bb = [0 for _ in range(n*m)]

for i in range(n):
    if aa[n*m-a[i]] == 0:
        aa[n*m-a[i]] = 1
    else:
        print(0)
        exit()

for j in range(m):
    if bb[n*m-b[j]] == 0:
        bb[n*m-b[j]] = 1
    else:
        print(0)
        exit()

for i in range(n*m):
    if i == 0:
        continue
    aa[i] += aa[i-1]
    bb[i] += bb[i-1]

aa = aa[::-1]
bb = bb[::-1]

seta = set(a)
setb = set(b)

ans = 1

for x in range(1, n*m+1)[::-1]:
    if x in seta and x in setb:
        continue
    elif x in seta and x not in setb:
        ans *= bb[x-1]
        ans %= mod
    elif x not in seta and x in setb:
        ans *= aa[x-1]
        ans %= mod
    elif aa[x-1]*bb[x-1] - (n*m-x) > 0:
        ans *= aa[x-1]*bb[x-1] - (n*m-x)
        ans %= mod
    else:
        print(0)
        exit()

print(ans)

Buggy python submission (verdict: Runtime Error):
mod = 10**9+7
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

a = sorted(a, reverse=True)
b = sorted(b, reverse=True)

c = [0 for _ in range(n*m)]
for i in range(n):
    c[a[i]-1] += 1
    if c[a[i]-1] == 2:
        print(0)
        exit()

c = [0 for _ in range(n*m)]
for i in range(m):
    c[b[i]-1] += 1
    if c[b[i]-1] == 2:
        print(0)
        exit()

ans = 1

i = 0
j = 0

while i < n and j < m:
    if n*m > max(a[i], b[j]) + i*j:
        print(0)
        exit()
    if a[i] == b[i]:
        i += 1
        j += 1
    elif a[i] > b[j]:
        ans *= i
        ans %= mod
        i += 1
    else:
        ans *= j
        ans %= mod
        j += 1

res = n*m - min(n, m)

for i in range(1, res+1):
    ans *= i
    ans %= mod

print(ans)


Fixed submission:
mod = 10**9+7
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

aa = [0 for _ in range(n*m)]
bb = [0 for _ in range(n*m)]

for i in range(n):
    if aa[n*m-a[i]] == 0:
        aa[n*m-a[i]] = 1
    else:
        print(0)
        exit()

for j in range(m):
    if bb[n*m-b[j]] == 0:
        bb[n*m-b[j]] = 1
    else:
        print(0)
        exit()

for i in range(n*m):
    if i == 0:
        continue
    aa[i] += aa[i-1]
    bb[i] += bb[i-1]

aa = aa[::-1]
bb = bb[::-1]

seta = set(a)
setb = set(b)

ans = 1

for x in range(1, n*m+1)[::-1]:
    if x in seta and x in setb:
        continue
    elif x in seta and x not in setb:
        ans *= bb[x-1]
        ans %= mod
    elif x not in seta and x in setb:
        ans *= aa[x-1]
        ans %= mod
    elif aa[x-1]*bb[x-1] - (n*m-x) > 0:
        ans *= aa[x-1]*bb[x-1] - (n*m-x)
        ans %= mod
    else:
        print(0)
        exit()

print(ans)