Harryxun/stf_run huggingface dataset:
import sys
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X.sort()

pos = [[0] * 2 for i in range(N)]
total = 0

if M <= N:
    print(0)
    sys.exit()

for i in range(N):
    pos[i][0] = i
    pos[i][1] = abs(X[i+1] - X[i])
    total = total + pos[i][1]

pos = sorted(pos, key=lambda x:(x[1]))

for j in range(N, M-1):
    dim = abs(X[j+1]-X[j])
    total = total + dim
    if pos[0][1] < dim:
        pos[0][0] = j
        pos[0][1] = dim
        pos = sorted(pos, key=lambda x:(x[1]))



pos = sorted(pos, key=lambda x:(x[1]), reverse = True)
for i in range(N-1):
    total = total - pos[i][1]

print(total)


### FIXED CODE ###

T, X = (int(i) for i in input().split())

print(T/X)
N, M = map(int, input().split())
A = [list(input()) for i in range(N)]
B = [list(input()) for i in range(M)]
ans = 'No'
for i in range(N - M +1):
    for j in range(N - M +1):
        count = 0
        for row in range(M):
            for col in range(M):
                if B[row][col] == A[i + row][j + col]:
                    count += 1
        if count == M * M:
            ans = 'Yes'

print(ans)

### FIXED CODE ###

a, b = (int(x) for x in input().split())
if a == b:
  print('Draw')
elif a == 1:
  print('Alice')
elif b == 1:
  print('Bob')
elif a > b:
  print('Alice')
elif a < b:
  print('Bob')
from scipy.special import comb

def c(a, b):
    return comb(a, b, exact=True)

n, k = map(int, input().split())
MOD = 10**9+7
memo = 0
for i in range(1, k+1):
    # tmp = c(n-k+i, i)
    # print(((tmp - memo) * c(k-1, i-1)) % MOD)
    # memo += tmp
    # memo %= MOD
    tmp = ((c(n-k+i, i) - memo) * c(k-1, i-1)) % MOD
    print(tmp)
    memo += tmp
    memo %= MOD


### FIXED CODE ###

from scipy.misc import comb

def c(a, b):
    return comb(a, b, exact=True)

n, k = map(int, input().split())
MOD = 10**9+7
memo = 0
for i in range(1, k+1):
    print((c(n-k+1, i) * c(k-1, i-1)) % MOD)
def LCS():
  x = raw_input()
  y = raw_input()
  X = len(x)
  Y = len(y)
  
  for i in range(X):
    c[i][0] = 0
  for j in range(Y):
    c[0][j] = 0
???x = ' ' + x
  y = ' ' + y

  for i in range(1,X+1):
    for j in range(1,Y+1):
      if X[i] == Y[i]: 
        c[i][j] = c[i-1][j-1] + 1

      elif c[i-1][j] >= c[i][j-1]:
        c[i][j] = c[i-1][j]
      else:
        c[i][j] = c[i][j-1]
  print c[X][Y]

q = input()


for i in range(q):
  LCS()

### FIXED CODE ###

def LCS():
    X = raw_input()
    Y = raw_input()
 
    a, b = len(X), len(Y)
    c1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = X[i]
        c2 = c1+[]
        for j in range(b):
            if e1 == Y[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
 
n = input()
for i in range(n):
    print LCS()
S = input()
Slist = list(S)
res = 0
for i in Slist:
  res += int(i)
print(int(S) % res = 0)

### FIXED CODE ###

S = int(input())
Slist = list(str(S))
res = 0
for i in Slist:
  res += int(i)
if S % res == 0:
  print("Yes")
else:
  print("No")
import sys


def solve():
    input = lambda: sys.stdin.readline().rstrip()
    sys.setrecursionlimit(10 ** 7)
    mod = 10 ** 9 + 7

    def comb(n, r):
        if r > n: return 0
        return fac[n] * inv[r] * inv[n - r] % mod

    def dfs(v, Pa=-1):
        for u in to[v]:
            if u == Pa: continue
            dfs(u, v)
            size[v] += size[u]
            dp[v] *= dp[u] % mod
            dp[v] *= inv[size[u]] % mod
        dp[v] *= fact[size[v]] % mod
        size[v] += 1

    def bfs(v, Pa=-1, P_val=1, P_sz=0):
        ans[v] = P_val * dp[v] * comb(n - 1, P_sz) % mod
        for u in to[v]:
            if u == Pa: continue
            val = ans[v] * INV(dp[u] * comb(n - 1, size[u])) % mod
            bfs(u, v, val, n - size[u])

    n = int(input())
    to = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        to[a].append(b)
        to[b].append(a)

    fac = [1] * (n + 2)
    inv = [1] * (n + 2)

    for i in range(2, n + 1):
        fac[i] = fac[i - 1] * i % mod
    INV = lambda x: pow(x, mod - 2, mod)
    inv[n] = INV(fac[n])
    for i in range(n - 1, 1, -1):
        inv[i] = inv[i + 1] * (i + 1) % mod

    dp = [1] * n
    size = [0] * n
    ans = [0] * n

    dfs(0)
    bfs(0)
    print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

### FIXED CODE ###

import sys

def solve():
    input = lambda: sys.stdin.readline().rstrip()
    sys.setrecursionlimit(10 ** 7)
    mod = 10 ** 9 + 7

    def comb(n, r):
        if r > n: return 0
        return fac[n] * inv[r] * inv[n - r] % mod

    def dfs(v, Pa=-1):
        for u in to[v]:
            if u == Pa: continue
            dfs(u, v)
            size[v] += size[u]
            dp[v] *= dp[u] % mod
            dp[v] *= inv[size[u]] % mod
        dp[v] *= fac[size[v]] % mod
        size[v] += 1

    def bfs(v, Pa=-1, P_val=1, P_sz=0):
        ans[v] = P_val * dp[v] * comb(n - 1, P_sz) % mod
        for u in to[v]:
            if u == Pa: continue
            val = ans[v] * INV(dp[u] * comb(n - 1, size[u])) % mod
            bfs(u, v, val, n - size[u])

    n = int(input())
    to = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        to[a].append(b)
        to[b].append(a)

    fac = [1] * (n + 2)
    inv = [1] * (n + 2)

    for i in range(2, n + 1):
        fac[i] = fac[i - 1] * i % mod
    INV = lambda x: pow(x, mod - 2, mod)
    inv[n] = INV(fac[n])
    for i in range(n - 1, 1, -1):
        inv[i] = inv[i + 1] * (i + 1) % mod

    dp = [1] * n
    size = [0] * n
    ans = [0] * n

    dfs(0)
    bfs(0)
    print(*ans, sep='\n')

if __name__ == '__main__':
    solve()
A,B = map(int, input().split())
print('Possible' if (A or B or A + B) % 3 = 0 else 'Impossible')

### FIXED CODE ###

A,B = map(int, input().split())
print('Possible' if (A % 3 == 0) or (B % 3 == 0) or ((A + B) % 3 == 0) else 'Impossible')
from collections import deque

h,w=map(int, input().split())  
s=[list(input()) for i in range(h)]
v=[]
def bfs():
    d = [[float("inf")] * w for i in range(h)]
    que = deque([])
    #黒だったらスタートキューに入れる
    for i in range(w):
        for j in range(h):
            if s[i][j]=='#':
                que.append((i, j))
                d[i][j]=0
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    
    
    while que:
        p = que.popleft()
         # 黒にする4方向をループ
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            if 0<=nx<w and 0<=ny<h and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
                
    for i in range(h):
        v.append(max(d[i]))
    return max(v)
res=bfs()
print(res)

### FIXED CODE ###

from collections import deque
 
h, w = map(int, input().split())
s = [input() for i in range(h)]
 
d = [[h+w for j in range(w)] for i in range(h)]
que = deque()
 
#黒だったらスタートキューに入れる
for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
        que.append((i, j))
        d[i][j] = 0
 
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
 
 
while que:
    p = que.popleft()
    # 黒にする4方向をループ
    for i in range(4):
        nx = p[1] + dx[i]
        ny = p[0] + dy[i]
 
        if (0<=nx<w) and (0<=ny<h) and (d[ny][nx]==h+w):
            que.append((ny, nx))
            d[ny][nx] = d[p[0]][p[1]] + 1
 
res = max(map(max, d))
print(res)
from collections import Counter

N = int(input())
a = sorted(list(map(int, input().split())))

M = 10**6 + 1

table = [1] * M

B = Counter(a)
Key = list(B.keys())

count = 0
for x in Key:
    if B[x] == 1 and table[x] == 1:
        count += 1

    for i in range(M):
        if i * x > M:
            break
        else:
            table[i * x] = 0

print(count)


### FIXED CODE ###

from collections import Counter

N = int(input())
a = sorted(list(map(int, input().split())))

M = 10**6

table = [1] * (M + 1)

B = Counter(a)
Key = list(B.keys())

count = 0
for x in Key:
    if B[x] == 1 and table[x] == 1:
        count += 1

    for i in range(M):
        if i * x > M:
            break
        else:
            table[i * x] = 0

print(count)
n = int(input())
high = list(map(int,input().split()))
lists = []

def func(i,t):
    if i == n - 1:
        lists.append(t)
        return
    if high[i] < high[i+1]:
        lists.append(t)
        func(i+1,0)
    else:
        t += 1
        func(i+1,t)

func(0,0)
print(max(lists))


### FIXED CODE ###

import sys
sys.setrecursionlimit(100000) 

n = int(input())
high = list(map(int,input().split()))
lists = []

def func(i,t):
    if i == n - 1:
        lists.append(t)
        return
    if high[i] < high[i+1]:
        lists.append(t)
        func(i+1,0)
    else:
        t += 1
        func(i+1,t)

func(0,0)
print(max(lists))
x,y = map(int, input().split())
a = [1,3,5,7,8,10,12]
b = [4,6,9,11]
if (x in a and y in a) or x (in b and y in b):
  print("Yes")
else:
  print("No")

### FIXED CODE ###

x,y = map(int, input().split())
a = [1,3,5,7,8,10,12]
b = [4,6,9,11]
if (x in a and y in a) or (x in b and y in b):
  print("Yes")
else:
  print("No")
n,m = map(int,input().split())
if m == 0:
    print(0, 0)
else:
    p = [0] * m
    s = [0] * m
    for i in range(m):
        p[i], s[i] = map(str, input().split())
    for i in range(m):
        p[i] = int(p[i])

    probcnt = p[0]
    ACval = False
    ACcnt = 0
    wa = 0
    pena = 0
    for i in range(m):
        if probcnt == p[i]:
            if ACval == False:
                if s[i] == 'WA':
                    wa += 1
                else:
                    ACval = True
                    ACcnt += 1
                    pena += wa
        wa = 0
        else:
            ACval = False
            probcnt = p[i]
            if ACval == False:
                if s[i] == 'WA':
                    wa += 1
                else:
                    ACval = True
                    ACcnt += 1
                    pena += wa
        wa = 0

    print(ACcnt, pena)


### FIXED CODE ###

n, m = map(int,input().split())
p = [0] * m
s = [0] * m
for i in range(m):
    p[i], s[i] = map(str, input().split())
for i in range(m):
    p[i] = int(p[i])

ans1 = 0
ans2 = 0

aclis = [0] * (n+1)
walis = [0] * (n+1)


for i in range(m):
    if s[i] == 'AC':
        aclis[p[i]] = 1



    elif aclis[p[i]] == 0 and s[i] == 'WA':
        walis[p[i]] += 1
ans1 = aclis.count(1)
#print(aclis)
#print(walis)

for i in range(n+1):
    if aclis[i] == 1:
        ans2 += walis[i]


print(ans1, ans2)
N, T = map(int,raw_input().split())
ans = float("inf")

for i in range(N):
    c,t = map(int,input(),split())
    if t <= T:
        ans = min(ans,c)

if ans == float("inf"):
    print("TLE")
else:
    print(ans)


### FIXED CODE ###

N, T = map(int,input().split())
ans = float("inf")

for i in range(N):
    c,t = map(int,input().split())
    if t <= T:
        ans = min(ans,c)

if ans == float("inf"):
    print("TLE")
else:
    print(ans)
dp = [0] * N

dp[0] = 0
dp[1] = abs(H[1] - H[0])
for i in range(2, N):
    dp[i] = min([
        dp[i-1] + abs(H[i] - H[i-1]),
        dp[i-2] + abs(H[i] - H[i-2]),
    ])
    
print(dp[-1])

### FIXED CODE ###

N = int(input())
H = list(map(int, input().split()))

dp = [0] * N

dp[0] = 0
dp[1] = abs(H[1] - H[0])
for i in range(2, N):
    dp[i] = min([
        dp[i-1] + abs(H[i] - H[i-1]),
        dp[i-2] + abs(H[i] - H[i-2]),
    ])
    
print(dp[-1])
H, W, M = map(int, input().split())
h = [0] * (H+1)
w = [0] * (W+1)
l = [[0]*(W+1) for i in range(H+1)]
for i in range(M):
  x, y = map(int, input().split())
  h[x] += 1
  w[y] += 1
  l[x][y] = 1
  
a = []
b = []
c = max(h)
d = max(w)
for i in range(H+1):
  if h[i] == c:
    a.append(i)
for i in range(W+1):
  if w[i] == d:
    b.append(i)

flag = False
for s in a:
  for t in b:
    if l[s][t] == 0:
      flag = True
      break
if flag:
  ans = c+d
else:
  ans = c+d-1
print(ans)


### FIXED CODE ###

H, W, M = map(int, input().split())
h = [0] * (H+1)
w = [0] * (W+1)
l = set()
for i in range(M):
  x, y = map(int, input().split())
  h[x] += 1
  w[y] += 1
  l.add((x, y))
  
c = max(h)
d = max(w)
a = [i for i, x in enumerate(h) if x == c]
b = [i for i, x in enumerate(w) if x == d]

flag = False
for s in a:
  for t in b:
    if (s, t) not in l:
      flag = True
      break
  if flag:
    break
ans = c+d
if not flag:
  ans -= 1
print(ans)
n = input()
n = int(n)
list = []

i = 0
while(i<n):
	x = input()
	x = int(x)
	list[i] = x
	i = i + 1

maxv = -20000000
minv = list[0]
i = 1
while(i<n):
	if(maxv >= list[i]-minv):
		maxv = maxv
	else:
		maxv = list[i]-minv
	
	if(minv <= list[i]):
		minv = minv
	else:
		minv = list[i]
	i = i + 1

	print(maxv)


### FIXED CODE ###

n = input()
n = int(n)
list = []

i = 0
while(i<n):
	x = input()
	x = int(x)
	list.append(x)
	i = i + 1

maxv = -2000000000
minv = list[0]
i = 1
while(i<n):
        maxv = max(maxv, list[i]-minv)
        minv = min(minv, list[i])
        i = i + 1
	
print(maxv)
N = int(input())

if N == 1:
	answer = "Hello World"
elif N == 2:
	A,B = map(int,input().split())
	answer = A + B

print(answer)

### FIXED CODE ###

N = int(input())

if N == 1:
	answer = "Hello World"
	print(answer)
elif N == 2:
	A = int(input())
	B = int(input())
	answer = A + B
	print(answer)
num=int(input())
for i in range (0,num):
	some=input()
    list1.append(some)
print(len(set(i for i in list1 if i.isalpha())))

### FIXED CODE ###

list1=[]
num=int(input())
for i in range (0,num):
    list1.append(input())
print(len(set(i for i in list1 if i.isalpha())))
N, M, K = map(int, input().split())

class UnionFind:
	def __init__(self, n):
		self.groups = [g for g in range(n)]

	def get_root(self, i):
		parent = self.groups[i]
		if parent == i:
			return parent
		else:
			root = self.get_root(parent)
			self.groups[i] = root
			return root
	
	def set_root(self, i, root):
		parent = self.groups[i]
		self.groups[i] = root
		if parent == i:
			return
		else:
			self.set_root(parent, root)
	
	def combine(self, i1, i2):
		self.set_root(i2, self.get_root(i1))
	
	def in_same_group(self, i1, i2):
		return self.get_root(i1) == self.get_root(i2)
	
	def counts(self):
		table = [None] * len(self.groups)
		for i in range(len(self.groups)):
			g = self.get_root(i)
			if table[g] != None:
				table[g] += 1
			else:
				table[g] = 1
		
		ret = [0] * len(self.groups)
		
		for i in range(len(self.groups)):
			ret[i] = table[self.get_root(i)]
		
		return ret
	
	def to_list(self):
		table = self.to_dic()
		return [table[g] for g in table]


groups = UnionFind(N)
minus_list = [0] * N

for _ in range(M):
	a, b = map(lambda x: int(x) - 1, input().split())
	groups.combine(a, b)
	minus_list[a] -= 1
	minus_list[b] -= 1

for _ in range(K):
	c, d = map(lambda x: int(x) - 1, input().split())
	if groups.in_same_group(c, d):
		minus_list[c] -= 1
		minus_list[d] -= 1

counts = groups.counts()

print(" ".join((str(counts[i] + minus_list[i] - 1) for i in range(N))))




### FIXED CODE ###

N, M, K = map(int, input().split())

class UnionFind:
	def __init__(self, n):
		self.groups = [g for g in range(n)]

	def get_root(self, i):
		parent = self.groups[i]
		if parent == i:
			return parent
		else:
			root = parent
			while True:
				tmp = self.groups[root]
				if tmp == root:
					break
				root = tmp
			
			while i != root:
				tmp = self.groups[i]
				self.groups[i] = root
				i = tmp
			
			return root
	
	def set_root(self, i, root):
		parent = self.groups[i]
		while parent != root:
			self.groups[i] = root
			i = parent
			parent = self.groups[i]
	
	def combine(self, i1, i2):
		self.set_root(i2, self.get_root(i1))
	
	def in_same_group(self, i1, i2):
		return self.get_root(i1) == self.get_root(i2)
	
	def counts(self):
		table = [None] * len(self.groups)
		for i in range(len(self.groups)):
			g = self.get_root(i)
			if table[g] != None:
				table[g] += 1
			else:
				table[g] = 1
		
		ret = [0] * len(self.groups)
		
		for i in range(len(self.groups)):
			ret[i] = table[self.get_root(i)]
		
		return ret
	
	def to_list(self):
		table = self.to_dic()
		return [table[g] for g in table]


groups = UnionFind(N)
minus_list = [0] * N

for _ in range(M):
	a, b = map(lambda x: int(x) - 1, input().split())
	groups.combine(a, b)
	minus_list[a] -= 1
	minus_list[b] -= 1

for _ in range(K):
	c, d = map(lambda x: int(x) - 1, input().split())
	if groups.in_same_group(c, d):
		minus_list[c] -= 1
		minus_list[d] -= 1

counts = groups.counts()

print(" ".join((str(counts[i] + minus_list[i] - 1) for i in range(N))))
n = int(input())
tree = [[] for i in range(n + 1)]
for i in range(n-1):
    u, v, w = map(int, input().split())
    tree[u].append([v,w])
    tree[v].append([u,w])
finished = set()
ans = [-1 for i in range(n + 1)]
def dfs(node, weight):
    finished.add(node)
    if weight % 2 == 0:
        ans[node] = 0
    else:
        ans[node] = 1
    for i in tree[node]:
        next, w = i
        if next not in finished:
            dfs(next, w + weight)
dfs(1,0)
for i in range(1, len(ans)):
    print(ans[i])

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
n = int(input())
tree = [[] for i in range(n + 1)]
for i in range(n-1):
    u, v, w = map(int, input().split())
    tree[u].append([v,w])
    tree[v].append([u,w])
finished = set()
ans = [-1 for i in range(n + 1)]
def dfs(node, weight):
    finished.add(node)
    if weight % 2 == 0:
        ans[node] = 0
    else:
        ans[node] = 1
    for i in tree[node]:
        next, w = i
        if next not in finished:
            dfs(next, w + weight)
dfs(1,0)
for i in range(1, len(ans)):
    print(ans[i])
while True:
	line = input().strip()
	if len(line)==0:break
	print(0)

"""

/*

0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0


0
1
28
*/

"""

### FIXED CODE ###

from __future__ import division
import sys
if sys.version_info[0]>=3: raw_input=input

X=4
Y=2
i=0
m={}
prev={}
v=[]
for i in range(X*Y): v.append(str(i))
m[''.join(v)]=[0,0]
prev[''.join(v)]=[''.join(v),None]
q=[v]
while len(q)>0:
	v=q.pop(0)
	coor=m[''.join(v)][0]
	x=coor%X
	y=coor//X
	depth=m[''.join(v)][1]
	nextstr=''.join(v)
	if 0<x:
		v[coor],v[coor-1]=v[coor-1],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor-1,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'R']
		v[coor],v[coor-1]=v[coor-1],v[coor]
	if x<X-1:
		v[coor],v[coor+1]=v[coor+1],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor+1,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'L']
		v[coor],v[coor+1]=v[coor+1],v[coor]
	if 0<y:
		v[coor],v[coor-X]=v[coor-X],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor-X,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'D']
		v[coor],v[coor-X]=v[coor-X],v[coor]
	if y<Y-1:
		v[coor],v[coor+X]=v[coor+X],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor+X,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'U']
		v[coor],v[coor+X]=v[coor+X],v[coor]

try:
	while True:
		v=raw_input().split()
		print(m[''.join(v)][1])
except EOFError:
	pass
n = int(input())
v = list(map(int,input().split()))

a = [v[2*i] for i in range(int(n/2))]
b = [v[2*i+1] for i in range(int(n/2))]

from collections import Counter

x = Counter(a)
y = Counter(b)
x = list(x.items())
y = list(y.items())
x.sort(reverse=1,key=lambda p: p[1])
y.sort(reverse=1,key=lambda p: p[1])

if x[0][0] != y[0][0]:
    print(n-x[0][1]-y[0][1])
else:
    if x[0][0] == y[0][0]:
        if min(len(x),len(y))>=2:
            print(n-max(x[0][1]+y[1][1],y[0][1]+x[1][1]))
        elif len(x)==2:
            print(n-x[1][1]-y[0][1])
        else:
            print(n-y[1][1]-x[0][1])

### FIXED CODE ###

n = int(input())
v = list(map(int,input().split()))

a = [v[2*i] for i in range(int(n/2))]
b = [v[2*i+1] for i in range(int(n/2))]

from collections import Counter

x = Counter(a)
y = Counter(b)
x = list(x.items())
y = list(y.items())
x.sort(reverse=1,key=lambda p: p[1])
y.sort(reverse=1,key=lambda p: p[1])

if x[0][0] != y[0][0]:
    print(n-x[0][1]-y[0][1])
else:
    if x[0][0] == y[0][0]:
        if min(len(x),len(y))>=2:
            print(n-max(x[0][1]+y[1][1],y[0][1]+x[1][1]))
        elif len(x)==2:
            print(n-x[1][1]-y[0][1])
        elif len(y)==2:
            print(n-y[1][1]-x[0][1])
        else:
            print(int(n/2))
N,M = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = [0 for i in range(N*M+1)]
b = [0 for i in range(N*M+1)]
da = [0 for i in range(N*M+1)]
mod = 10**9+7
for i in range(N):
    a[A[i]] += 1
    if da[A[i]] == 1:
        print(0)
        quit()
    da[A[i]] += 1
for i in range(M):
    b[B[i]] += 1
    if db[B[i]] == 1:
        print(0)
        quit()
    db[B[i]] += 1
for i in range(1,N*M+1):
    a[-i-1] += a[-i]
    b[-i-1] += b[-i]
ans = 1
for i in range(N*M):
    f = 0
    g = 0
    x = N*M - i
    y = i
    if da[x] == 1:
        f = 1
    if db[x] == 1:
        g = 1
    z = 1
    if f == 0:
        z *= a[x]
    if g == 0:
        z *= b[x]
    if f+g == 0:
        z -= i
    if z <= 0:
        print(0)
        quit()
    else:
        ans *= z%mod
print(ans%mod)

### FIXED CODE ###

N,M = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = [0 for i in range(N*M+1)]
b = [0 for i in range(N*M+1)]
da = [0 for i in range(N*M+1)]
db = [0 for i in range(N*M+1)]
mod = 10**9+7
for i in range(N):
    a[A[i]] += 1
    if da[A[i]] == 1:
        print(0)
        quit()
    da[A[i]] += 1
for i in range(M):
    b[B[i]] += 1
    if db[B[i]] == 1:
        print(0)
        quit()
    db[B[i]] += 1
for i in range(1,N*M+1):
    a[-i-1] += a[-i]
    b[-i-1] += b[-i]
ans = 1
for i in range(N*M):
    f = 0
    g = 0
    x = N*M - i
    y = i
    if da[x] == 1:
        f = 1
    if db[x] == 1:
        g = 1
    z = 1
    if f == 0:
        z = z*a[x]%mod
    if g == 0:
        z = z*b[x]%mod
    if f+g == 0:
        z -= i
    if z <= 0:
        print(0)
        quit()
    else:
        ans = ans*z%mod
print(ans%mod)
S = int(input())
h = S//3600
m = (S - h)//60
s = S - (h*3600) - (m*60)
print(h + ":" + m + ":" + s)

### FIXED CODE ###

S = int(input())
h = (S//3600)
m = (S - (h*3600))//60
b = S - (h*3600) - (m*60)
print(str(h)+":" +str(m) + ":" + str(b))
s = input()
s = s.replace("/","")
print("Heisei" if s <= 20190430 else "TBD")

### FIXED CODE ###

s = input()
s = s.replace("/","")
s = int(s)
print("Heisei" if s <= 20190430 else "TBD")
n=int(inoput())
a=list(map(int,input()))
total=0
for i in range(0,n-1):
  for j in range(1,n):
    total+=a[i]*a[j]
print(total%(10**9+7))
    

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
x=0
for i in range(n):
  x+=a[i]
x=x**2
y=0
for j in range(n):
  y+=a[j]**2
z=(x-y)//2
print(z%(10**9+7))
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]

dxy=[(-1,0),(0,1),(0,-1),(1,0)]
for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      f=False
      for x,y in dxy:
        
        if 0<=i+x<=h-1 and 0<=j+y<=w-1 and s[i+x][j+y]=="#":
          f=True
      
      if not f:
        print("No")
        break
        
        
if f:
  print("Yes")





### FIXED CODE ###

h,w=map(int,input().split())
s=[list(input()) for i in range(h)]

dxy=[(-1,0),(0,1),(0,-1),(1,0)]
for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      f=False
      for x,y in dxy:
        
        if 0<=i+x<=h-1 and 0<=j+y<=w-1 and s[i+x][j+y]=="#":
          f=True
      
      if not f:
        print("No")
        exit()
        
        

print("Yes")