Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
a.sort()
b.sort()

q = 1
for x in range(n*m, 0, -1):
  if x in a and x in b:
    q *= 1
  elif x in a:
    q = q * max(m - bisect(b, x), 0)  % p
  elif x in b:
    q = q * max(n - bisect(a, x), 0) % p
  else:
    u = bisect(a, x)
    v = bisect(b, x)
    q = q * (max((n-u) * (m-v) - (n * m - x), 0) % p) % p
print(q)
    
    
    

Fixed submission:
from bisect import bisect
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
A = set(a)
B = set(b)
a.sort()
b.sort()

q = 1
for x in range(n*m, 0, -1):
  if x in A and x in B:
    q *= 1
  elif x in A:
    q = q * max(m - bisect(b, x), 0)  % p
  elif x in B:
    q = q * max(n - bisect(a, x), 0) % p
  else:
    u = bisect(a, x)
    v = bisect(b, x)
    q = q * (max((n-u) * (m-v) - (n * m - x), 0) % p) % p
print(q)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
a.sort()
b.sort()

q = 1
for x in range(n*m, 0, -1):
  if x in a and x in b:
    q *= 1
  elif x in a:
    q = q * max(m - bisect(b, x), 0)  % p
  elif x in b:
    q = q * max(n - bisect(a, x), 0) % p
  else:
    u = bisect(a, x)
    v = bisect(b, x)
    q = q * max((n-u) * (m-v) - (n * m - x), 0) % p
print(q)
    
    
    

Fixed submission:
from bisect import bisect
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
A = set(a)
B = set(b)
a.sort()
b.sort()

q = 1
for x in range(n*m, 0, -1):
  if x in A and x in B:
    q *= 1
  elif x in A:
    q = q * max(m - bisect(b, x), 0)  % p
  elif x in B:
    q = q * max(n - bisect(a, x), 0) % p
  else:
    u = bisect(a, x)
    v = bisect(b, x)
    q = q * (max((n-u) * (m-v) - (n * m - x), 0) % p) % p
print(q)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
a.sort()
b.sort()
a.reverse()
b.reverse()

a = a + [0]
b = b + [0]
max_a = 0
max_b = 0
q = 1
for x in range(n*m, 0, -1):
  if a[max_a] >= x:
    max_a += 1
  if b[max_b] >= x:
    max_b += 1
  if x in a and x in b:
    q *= 1
  elif x in a:
    q = q * max_b % p
  elif x in b:
    q = q * max_a % p
  else:
    q = q * (max_a * max_b - (n * m - x)) % p
print(q)
    
    
    

Fixed submission:
from bisect import bisect
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
A = set(a)
B = set(b)
a.sort()
b.sort()

q = 1
for x in range(n*m, 0, -1):
  if x in A and x in B:
    q *= 1
  elif x in A:
    q = q * max(m - bisect(b, x), 0)  % p
  elif x in B:
    q = q * max(n - bisect(a, x), 0) % p
  else:
    u = bisect(a, x)
    v = bisect(b, x)
    q = q * (max((n-u) * (m-v) - (n * m - x), 0) % p) % p
print(q)