Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.sort()
b.sort()
ia = 0
ib = 0
cnt = 1
for i in range(1,n*m+1)[::-1]:
    if i in a:
        if i in b:
            cnt *= 1
            ia += 1
            ib += 1
        else:
            cnt *= ib
            cnt %= 1000000007
            ia += 1
    else:
        if i in b:
            cnt *= ia
            cnt %= 1000000007
            ib += 1
        else:
            cnt *= ia*ib - (n*m - i)
            cnt %= 1000000007
if len(a) != len(set(a)) or len(b) != len(set(b)) :
    cnt = 0
print(cnt)


Fixed submission:
from bisect import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.append(0)
b.append(0)
a.sort()
b.sort()
ia = 0
ib = 0
cnt = 1
for i in range(1,n*m+1)[::-1]:
    if i == a[-1]:
        if i == b[-1]:
            cnt *= 1
            a.pop(-1)
            b.pop(-1)
            ia += 1
            ib += 1
        else:
            cnt *= ib
            a.pop(-1)
            cnt %= 1000000007
            ia += 1
    else:
        if i == b[-1]:
            cnt *= ia
            b.pop(-1)
            cnt %= 1000000007
            ib += 1
        else:
            cnt *= ia*ib - (n*m - i)
            cnt %= 1000000007

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.sort()
b.sort()
cnt = 1
for i in range(1,n*m+1)[::-1]:
    if i in a:
        if i in b:
            cnt *= 1
        else:
            cnt *= m-bisect(b,i)
            cnt %= 1000000007
    else:
        if i in b:
            cnt *= n-bisect(a,i)
            cnt %= 1000000007
        else:
            cnt *= (n - bisect(a,i))*(m - bisect(b,i)) - (n*m - i)
            cnt %= 1000000007
if len(a) != len(set(a)) or len(b) != len(set(b)) :
    cnt = 0
print(cnt)


Fixed submission:
from bisect import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.append(0)
b.append(0)
a.sort()
b.sort()
ia = 0
ib = 0
cnt = 1
for i in range(1,n*m+1)[::-1]:
    if i == a[-1]:
        if i == b[-1]:
            cnt *= 1
            a.pop(-1)
            b.pop(-1)
            ia += 1
            ib += 1
        else:
            cnt *= ib
            a.pop(-1)
            cnt %= 1000000007
            ia += 1
    else:
        if i == b[-1]:
            cnt *= ia
            b.pop(-1)
            cnt %= 1000000007
            ib += 1
        else:
            cnt *= ia*ib - (n*m - i)
            cnt %= 1000000007

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

cnt = 1
for i in range(1,n*m+1)[::-1]:
    if i in a:
        if i in b:
            cnt *= 1
        else:
            cnt *= len([0 for j in b if i < j])
            cnt %= 1000000007
    else:
        if i in b:
            cnt *= len([0 for j in a if i < j])
            cnt %= 1000000007
        else:
            cnt *= len([0 for j in b if i < j])*len([0 for j in a if i < j]) - (n*m - i)
            cnt %= 1000000007
if len(a) != len(set(a)) or len(b) != len(set(b)) :
    cnt = 0
print(cnt)


Fixed submission:
from bisect import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.append(0)
b.append(0)
a.sort()
b.sort()
ia = 0
ib = 0
cnt = 1
for i in range(1,n*m+1)[::-1]:
    if i == a[-1]:
        if i == b[-1]:
            cnt *= 1
            a.pop(-1)
            b.pop(-1)
            ia += 1
            ib += 1
        else:
            cnt *= ib
            a.pop(-1)
            cnt %= 1000000007
            ia += 1
    else:
        if i == b[-1]:
            cnt *= ia
            b.pop(-1)
            cnt %= 1000000007
            ib += 1
        else:
            cnt *= ia*ib - (n*m - i)
            cnt %= 1000000007

print(cnt)