Buggy python submission (verdict: Runtime Error):
MOD = 10 ** 9 + 7
N, M = list(map(int, input().split()))
A = sorted(map(int, input().split()), reverse=True) + [-1]
B = sorted(map(int, input().split()), reverse=True) + [-1]

def solve():
    su = 1
    h, w = 0, 0
    for i, n in enumerate(range(N*M, 0, -1)):
        #print("h={}, w={}, i={}, n={}, su={}".format(h, w, i, n, su))
        if su == 0:
            return su
        if A[h] >= n:
            nh = h + 1
        if B[w] >= n:
            nw = w + 1
        if A[h] == B[w] and B[w] == n:
            #print(1)
            pass
        elif A[h] == n:
            #print(2)
            su = (su * nw) % MOD
        elif B[w] == n:
            #print(3)
            su = (su * nh) % MOD
        else:
            #print(4)
            su = (su * (nh * nw - i)) % MOD
        h = nh
        w = nw
    return su
        
            
if any(any(x1 == x2 for x1, x2 in zip(X, X[1:])) for X in [A, B]):
    print(0)
else:
    print(solve())


Fixed submission:
MOD = 10 ** 9 + 7
N, M = list(map(int, input().split()))
A = sorted(map(int, input().split()), reverse=True) + [-1]
B = sorted(map(int, input().split()), reverse=True) + [-1]

def solve():
    su = 1
    h, w = 0, 0
    for i, n in enumerate(range(N*M, 0, -1)):
        #print("h={}, w={}, i={}, n={}, su={}".format(h, w, i, n, su))
        if su == 0:
            return su
        nh, nw = h, w
        if A[h] >= n:
            nh = h + 1
        if B[w] >= n:
            nw = w + 1
        if A[h] == B[w] and B[w] == n:
            #print(1)
            pass
        elif A[h] == n:
            #print(2)
            su = (su * nw) % MOD
        elif B[w] == n:
            #print(3)
            su = (su * nh) % MOD
        else:
            #print(4)
            su = (su * ((nh * nw - i) % MOD)) % MOD
        h = nh
        w = nw
    return su
        
            
if any(any(x1 == x2 for x1, x2 in zip(X, X[1:])) for X in [A, B]):
    print(0)
else:
    print(solve())

Buggy python submission (verdict: Runtime Error):
MOD = 10 ** 9 + 7
N, M = list(map(int, input().split()))
A = sorted(map(int, input().split()), reverse=True) + [-1]
B = sorted(map(int, input().split()), reverse=True) + [-1]

if any(any(x1 == x2 for x1, x2 in zip(X, X[1:])) for X in [A, B]):
    import sys
    print(0)
    sys.exit()

#print(A)
#print()

def solve():
    su = 1
    h, w = 0, 0
    for i, n in enumerate(range(N*M, 0, -1)):
        #print("h={}, w={}, i={}, n={}, su={}".format(h, w, i, n, su))
        if A[h] >= n:
            nh = h + 1
        if B[w] >= n:
            nw = w + 1
        if A[h] == B[w] and B[w] == n:
            #print(1)
            pass
        elif A[h] == n:
            #print(2)
            su = (su * nw) % MOD
        elif B[w] == n:
            #print(3)
            su = (su * nh) % MOD
        else:
            #print(4)
            su = (su * (nh * nw - i)) % MOD
        h = nh
        w = nw
    return su
        
            
if __name__ == "__main__":
    print(solve())


Fixed submission:
MOD = 10 ** 9 + 7
N, M = list(map(int, input().split()))
A = sorted(map(int, input().split()), reverse=True) + [-1]
B = sorted(map(int, input().split()), reverse=True) + [-1]

def solve():
    su = 1
    h, w = 0, 0
    for i, n in enumerate(range(N*M, 0, -1)):
        #print("h={}, w={}, i={}, n={}, su={}".format(h, w, i, n, su))
        if su == 0:
            return su
        nh, nw = h, w
        if A[h] >= n:
            nh = h + 1
        if B[w] >= n:
            nw = w + 1
        if A[h] == B[w] and B[w] == n:
            #print(1)
            pass
        elif A[h] == n:
            #print(2)
            su = (su * nw) % MOD
        elif B[w] == n:
            #print(3)
            su = (su * nh) % MOD
        else:
            #print(4)
            su = (su * ((nh * nw - i) % MOD)) % MOD
        h = nh
        w = nw
    return su
        
            
if any(any(x1 == x2 for x1, x2 in zip(X, X[1:])) for X in [A, B]):
    print(0)
else:
    print(solve())