Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys

SIZE=100000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()

for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)            
        
        

Fixed submission:
# coding: utf-8
# Your code here!
import sys

SIZE=1000000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()
for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys

SIZE=100000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()

for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while(cn<n-1 or cm<m-1):
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
    else:
        print(0)
        sys.exit()
print(ans)            
        
        

Fixed submission:
# coding: utf-8
# Your code here!
import sys

SIZE=1000000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()
for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys

SIZE=100000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

cn = 0; cm=0
ans = 1
num=0
if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()

for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
while(cn<n-1 or cm<m-1):
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
        
print(ans)            
        
        

Fixed submission:
# coding: utf-8
# Your code here!
import sys

SIZE=1000000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()
for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys

SIZE=10000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

cn = 0; cm=0
ans = 1
num=0
if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()

for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
while(cn<n-1 or cm<m-1):
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
        
print(ans)            
        
        

Fixed submission:
# coding: utf-8
# Your code here!
import sys

SIZE=1000000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()
for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys

SIZE=10000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

cn = 0; cm=0
ans = 1
num=0
if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()

while(cn<n-1 or cm<m-1):
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if a[cn] == a[cn+1]:
            ans = 0
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if b[cm] == b[cm+1]:
            ans = 0
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if a[cn] == a[cn+1]:
            ans = 0
        if b[cm] == b[cm+1]:
            ans = 0
        if ans == 0:
            break
        cn += 1
        cm += 1
        

#print(a)
#print(b)
print(ans)            
        
        

Fixed submission:
# coding: utf-8
# Your code here!
import sys

SIZE=1000000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()
for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys

SIZE=10000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

cn = 0; cm=0
ans = 1
num=0
if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit

while(cn<n-1 or cm<m-1):
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if a[cn] == a[cn+1]:
            ans = 0
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if b[cm] == b[cm+1]:
            ans = 0
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if a[cn] == a[cn+1]:
            ans = 0
        if b[cm] == b[cm+1]:
            ans = 0
        if ans == 0:
            break
        cn += 1
        cm += 1
        

#print(a)
#print(b)
print(ans)            
        
        

Fixed submission:
# coding: utf-8
# Your code here!
import sys

SIZE=1000000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()
for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys

SIZE=10000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

cn = 0; cm=0
ans = 1
num=0
if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit

while(cn<n-1 or cm<m-1):
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if a[cn] == a[cn+1]:
            ans = 0
        if ans == 0:
            break
        cn += 1

    if a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if b[cm] == b[cm+1]:
            ans = 0
        if ans == 0:
            break
        cm += 1

    if a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num)
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if a[cn] == a[cn+1]:
            ans = 0
        if b[cm] == b[cm+1]:
            ans = 0
        if ans == 0:
            break
        cn += 1
        cm += 1
        

#print(a)
#print(b)
print(ans)            
        
        

Fixed submission:
# coding: utf-8
# Your code here!
import sys

SIZE=1000000; MOD = 10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

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()

if a[n-1] != n*m or b[m-1] != n*m:
    print(0)
    sys.exit()
for i in range(n-1):
    if a[i] == a[i+1]:
        print(0)
        sys.exit()
for i in range(m-1):
    if b[i] == b[i+1]:
        print(0)
        sys.exit()

#print(a)
#print(b)
cn = 0; cm=0
ans = 1
num=0
while cn<n-1 or cm<m-1:
    if a[cn] < b[cm]:
        ans = ans * choose(a[cn]-num-1,m-cm-1) % MOD
        ans = ans * fac[m-cm] % MOD
        num += m-cm
#        print(ans, '<', cn,cm,num)
        if ans == 0:
            break
        cn += 1

    elif a[cn] > b[cm]:
        ans = ans * choose(b[cm]-num-1,n-cn-1) % MOD
        ans = ans * fac[n-cn] % MOD
        num += n-cn
#        print(ans, '>',cn,cm,num)
        if ans == 0:
            break
        cm += 1

    elif a[cn] == b[cm]:
#        print(ans, '=',cn,cm,num,'before')
        ans = ans * choose(a[cn]-num-1,m-cm + n-cn -2) % MOD
        ans = ans * fac[m-cm + n-cn -2] % MOD
        num += m-cm + n-cn - 1
#        print(ans, '=',cn,cm,num)
        if ans == 0:
            break
        cn += 1
        cm += 1
print(ans)