Buggy python submission (verdict: Time Limit Exceeded):
S = input().split()
N = int(S[0])
M = int(S[1])
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_MAX = max(A)
B_MAX = max(B)
MAX = A_MAX if (A_MAX > B_MAX) else B_MAX

mod = int(1e9 + 7)

A_F_cnt = 0
B_F_cnt = 0

result = 1
sA = [ False for _ in range(MAX+1) ]
sB = [ False for _ in range(MAX+1) ]

for i in range(len(A)):
    sA[A[i]] = True
for i in range(len(B)):
    sB[B[i]] = True


for i in range(MAX, 0, -1):
    in_A = sA[i]
    in_B = sB[i]
    if (in_A and in_B):
        A_F_cnt += 1
        B_F_cnt += 1
    elif (in_A):
        result *= B_F_cnt
        A_F_cnt += 1 
    elif (in_B):
        result *= A_F_cnt
        B_F_cnt += 1
    else:
        coef = ( ( A_F_cnt * B_F_cnt )%mod - (N*M - i) )
        if coef < 0:
            coef += mod
        result *= coef
        
    # print(in_A, in_B, A_F_cnt, B_F_cnt, result)

print(result % mod)



Fixed submission:
S = input().split()
N = int(S[0])
M = int(S[1])
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_MAX = max(A)
B_MAX = max(B)
MAX = A_MAX if (A_MAX > B_MAX) else B_MAX

mod = int(1e9 + 7)

A_F_cnt = 0
B_F_cnt = 0

result = 1
sA = [ False for _ in range(MAX+1) ]
sB = [ False for _ in range(MAX+1) ]

for i in range(len(A)):
    sA[A[i]] = True
for i in range(len(B)):
    sB[B[i]] = True


for i in range(MAX, 0, -1):
    in_A = sA[i]
    in_B = sB[i]
    if (in_A and in_B):
        A_F_cnt += 1
        B_F_cnt += 1
    elif (in_A):
        result *= B_F_cnt
        result %= mod
        A_F_cnt += 1 
    elif (in_B):
        result *= A_F_cnt
        result %= mod
        B_F_cnt += 1
    else:
        coef = ( ( A_F_cnt * B_F_cnt )%mod - (N*M - i) )
        if coef < 0:
            coef += mod
        result *= coef
        result %= mod
        
    # print(in_A, in_B, A_F_cnt, B_F_cnt, result)

print(result % mod)

Buggy python submission (verdict: Time Limit Exceeded):
S = input().split()
N = int(S[0])
M = int(S[1])
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_MAX = max(A)
B_MAX = max(B)
MAX = A_MAX if (A_MAX > B_MAX) else B_MAX

mod = int(1e9 + 7)

A_F_cnt = 0
B_F_cnt = 0

result = 1
sA = set(A)
sB = set(B)

for i in range(MAX, 0, -1):
    in_A = (i in sA)
    in_B = (i in sB)
    if (in_A and in_B):
        A_F_cnt += 1
        B_F_cnt += 1
    elif (in_A):
        result *= B_F_cnt
        A_F_cnt += 1 
    elif (in_B):
        result *= A_F_cnt
        B_F_cnt += 1
    else:
        coef = ( ( A_F_cnt * B_F_cnt )%mod - (N*M - i) )
        if coef < 0:
            coef += mod
        result *= coef
        
    # print(in_A, in_B, A_F_cnt, B_F_cnt, result)

print(result % mod)



Fixed submission:
S = input().split()
N = int(S[0])
M = int(S[1])
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_MAX = max(A)
B_MAX = max(B)
MAX = A_MAX if (A_MAX > B_MAX) else B_MAX

mod = int(1e9 + 7)

A_F_cnt = 0
B_F_cnt = 0

result = 1
sA = [ False for _ in range(MAX+1) ]
sB = [ False for _ in range(MAX+1) ]

for i in range(len(A)):
    sA[A[i]] = True
for i in range(len(B)):
    sB[B[i]] = True


for i in range(MAX, 0, -1):
    in_A = sA[i]
    in_B = sB[i]
    if (in_A and in_B):
        A_F_cnt += 1
        B_F_cnt += 1
    elif (in_A):
        result *= B_F_cnt
        result %= mod
        A_F_cnt += 1 
    elif (in_B):
        result *= A_F_cnt
        result %= mod
        B_F_cnt += 1
    else:
        coef = ( ( A_F_cnt * B_F_cnt )%mod - (N*M - i) )
        if coef < 0:
            coef += mod
        result *= coef
        result %= mod
        
    # print(in_A, in_B, A_F_cnt, B_F_cnt, result)

print(result % mod)

Buggy python submission (verdict: Time Limit Exceeded):
S = input().split()
N = int(S[0])
M = int(S[1])
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_MAX = max(A)
B_MAX = max(B)
MAX = A_MAX if (A_MAX > B_MAX) else B_MAX

mod = int(1e9 + 7)

A_F_cnt = 0
B_F_cnt = 0

result = 1

for i in range(MAX, 0, -1):
    in_A = (i in A)
    in_B = (i in B)
    if (in_A and in_B):
        A_F_cnt += 1
        B_F_cnt += 1
    elif (in_A):
        result *= B_F_cnt
        A_F_cnt += 1 
    elif (in_B):
        result *= A_F_cnt
        B_F_cnt += 1
    else:
        coef = ( ( A_F_cnt * B_F_cnt )%mod - (N*M - i) )
        if coef < 0:
            coef += mod
        result *= coef
        
    # print(in_A, in_B, A_F_cnt, B_F_cnt, result)

print(result % mod)

Fixed submission:
S = input().split()
N = int(S[0])
M = int(S[1])
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_MAX = max(A)
B_MAX = max(B)
MAX = A_MAX if (A_MAX > B_MAX) else B_MAX

mod = int(1e9 + 7)

A_F_cnt = 0
B_F_cnt = 0

result = 1
sA = [ False for _ in range(MAX+1) ]
sB = [ False for _ in range(MAX+1) ]

for i in range(len(A)):
    sA[A[i]] = True
for i in range(len(B)):
    sB[B[i]] = True


for i in range(MAX, 0, -1):
    in_A = sA[i]
    in_B = sB[i]
    if (in_A and in_B):
        A_F_cnt += 1
        B_F_cnt += 1
    elif (in_A):
        result *= B_F_cnt
        result %= mod
        A_F_cnt += 1 
    elif (in_B):
        result *= A_F_cnt
        result %= mod
        B_F_cnt += 1
    else:
        coef = ( ( A_F_cnt * B_F_cnt )%mod - (N*M - i) )
        if coef < 0:
            coef += mod
        result *= coef
        result %= mod
        
    # print(in_A, in_B, A_F_cnt, B_F_cnt, result)

print(result % mod)