Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from math import gcd

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

if n == 2:
    print(max(a))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(A[0], A[1]), gcd(A[1], A[2]), gcd(A[2], A[0]))
    # リストaの最初3個の最大公約数
    a = gcd(A[0], gcd(A[1], A[2]))

    for i in range(3, n):
        r = max(gcd(A[i], r), a)
        a = gcd(A[i], a)

    print(r)

Fixed submission:
from math import gcd

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

if n == 2:
    print(max(A))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(A[0], A[1]), gcd(A[1], A[2]), gcd(A[2], A[0]))
    # リストaの最初3個の最大公約数
    a = gcd(A[0], gcd(A[1], A[2]))

    for i in range(3, n):
        r = max(gcd(A[i], r), a)
        a = gcd(A[i], a)

    print(r)

Buggy python submission (verdict: Runtime Error):
from math import gcd

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

if n == 2:
    print(max(a))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(a[0], a[1]), gcd(a[1], a[2]), gcd(a[2], a[0]))
    # リストaの最初3個の最大公約数
    a = gcd(a[0], gcd(a[1], a[2]))

    for i in range(3, n):
        r = max(gcd(a[i], r), a)
        a = gcd(a[i], a)

    print(r)

Fixed submission:
from math import gcd

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

if n == 2:
    print(max(A))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(A[0], A[1]), gcd(A[1], A[2]), gcd(A[2], A[0]))
    # リストaの最初3個の最大公約数
    a = gcd(A[0], gcd(A[1], A[2]))

    for i in range(3, n):
        r = max(gcd(A[i], r), a)
        a = gcd(A[i], a)

    print(r)
Buggy python submission (verdict: Runtime Error):
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
sum = 0
people = 0

if x == sum(a):
    print(n)
elif x > sum(a):
    print(n-1)
else:
    for i in a:
        sum += i
        people += 1
        if sum > x:
            print(people-1)
            break

Fixed submission:
N, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
if x == sum(a):
    print(N)
elif x > sum(a):
    print(N-1)
else:
    for i in range(N):
        if sum(a[:i+1]) > x:
            print(i)
            break
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = []

for num in range(2, n//2):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))


Fixed submission:
n = int(input())
s = []

for num in range(1, n//2+1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))

Buggy python submission (verdict: Runtime Error):
n = int(input())
s = []

for num in range(2, n//2+1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))


Fixed submission:
n = int(input())
s = []

for num in range(1, n//2+1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))

Buggy python submission (verdict: Runtime Error):
n = int(input())
s = []

for num in range(2, n-1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))


Fixed submission:
n = int(input())
s = []

for num in range(1, n//2+1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))
Buggy python submission (verdict: Runtime Error):
n, y = map(int, input().split())
k = 0
result = 0

for i in range(n + 1):
    for j in range(n + 1):
        k = n - i - j
        ans = 10000 * i + 5000 * j + 1000 * k
        if (ans == y):
            print(a, b, c)
            result += 1
            exit()
            
if result == 0:
    print('-1 -1 -1')


Fixed submission:
n, y = map(int, input().split())
result = 0

for i in range(n+1):
    for j in range(n+1):
        k = n - i - j
        z = 10000*i + 5000*j + 1000*k
        if z == y and k >= 0:
            result += 1
            print(i, j, k)
            exit()

if result == 0:
    print('-1 -1 -1')

Buggy python submission (verdict: Runtime Error):
N,Y = map(int,input().split())
for x in range(N+1):
  for y in range(N-x+1):
    if 10000*x+5000*y+1000*(N-x-y) == Y:
      print(x,y,N-x-y)
      exit()
print(-1,-1,-1)
N,Y = map(int,input().split())
for x in range(N+1):
  for y in range(N-x+1):
    if 10000*x+5000*y+1000*(N-x-y) == Y:
      print(x,y,N-x-y)
      exit()
print(-1,-1,-1)


Fixed submission:
n, y = map(int, input().split())
result = 0

for i in range(n+1):
    for j in range(n+1):
        k = n - i - j
        z = 10000*i + 5000*j + 1000*k
        if z == y and k >= 0:
            result += 1
            print(i, j, k)
            exit()

if result == 0:
    print('-1 -1 -1')

Buggy python submission (verdict: Runtime Error):
n, y = map(int, input())
k = 0

for i in range(n+1):
  for j in range(n+1):
    k = n - (i + j)
    z = 10000*i + 5000*j + 1000*k
    if z == y:
      print(i, j, k)
      exit()

print(-1, -1, -1)

Fixed submission:
n, y = map(int, input().split())
result = 0

for i in range(n+1):
    for j in range(n+1):
        k = n - i - j
        z = 10000*i + 5000*j + 1000*k
        if z == y and k >= 0:
            result += 1
            print(i, j, k)
            exit()

if result == 0:
    print('-1 -1 -1')

Buggy python submission (verdict: Time Limit Exceeded):
n, y = map(int, input().split())
result = 0

for i in range(2001):
    for j in range(2001):
        for k in range(2001):
            z = y - 10000*i - 5000*j - 1000*k
            if z == 0:
                if n == i + j + k:
                    print(i, j, k)
                    result += 1
                    break

if result == 0:
    print('-1 -1 -1')

Fixed submission:
n, y = map(int, input().split())
result = 0

for i in range(n+1):
    for j in range(n+1):
        k = n - i - j
        z = 10000*i + 5000*j + 1000*k
        if z == y and k >= 0:
            result += 1
            print(i, j, k)
            exit()

if result == 0:
    print('-1 -1 -1')
Buggy python submission (verdict: Runtime Error):
N = int(input())
A, B = map(int, input().split())
result = 0

for num in range(1, N+1):
    num_1000 = num // 1000
    num_100 = (num - 1000*num_1000) // 100
    num_10 = (num - 1000*num_1000 - 100*num_100) // 10
    num_1 = num - 1000*num_1000 - 100*num_100 - 10*num_10
    sum = num_1 + num_10 + num_100 + num_1000
    if A <= sum <= B:
        result += num

print(result)

Fixed submission:
N, A, B = map(int, input().split())
result = 0

for num in range(1, N+1):
    num_10000 = num // 10000
    num_1000 = (num - 10000*num_10000) // 1000
    num_100 = (num - 10000*num_10000 - 1000*num_1000) // 100
    num_10 = (num - 10000*num_10000 - 1000*num_1000 - 100*num_100) // 10
    num_1 = num - 10000*num_10000 - 1000*num_1000 - 100*num_100 - 10*num_10
    sum = num_1 + num_10 + num_100 + num_1000 + num_10000
    if A <= sum <= B:
        result += num
        
print(result)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A, B = map(int, input().split())
result = 0

for num in range(1, N+1):
    num_1000 = num // 1000
    num_100 = (num - 1000*num_1000) // 100
    num_10 = (num - 1000*num_1000 - 100*num_100) // 10
    num_1 = num - 1000*num_1000 - 100*num_100 - 10*num_10
    sum = num_1 + num_10 + num_100 + num_1000
    if A <= sum <= B:
        result += num

print(result)

Fixed submission:
N, A, B = map(int, input().split())
result = 0

for num in range(1, N+1):
    num_10000 = num // 10000
    num_1000 = (num - 10000*num_10000) // 1000
    num_100 = (num - 10000*num_10000 - 1000*num_1000) // 100
    num_10 = (num - 10000*num_10000 - 1000*num_1000 - 100*num_100) // 10
    num_1 = num - 10000*num_10000 - 1000*num_1000 - 100*num_100 - 10*num_10
    sum = num_1 + num_10 + num_100 + num_1000 + num_10000
    if A <= sum <= B:
        result += num
        
print(result)
Buggy python submission (verdict: Runtime Error):
s = input()
t = input()
s = list(s)
t = list(t)

s.sort(reverse=True)
s_final = s[-1]
s_semifinal = s[-2]
s_len = len(s)
t.sort(reverse=True)
t_first = t[0]
t_second = t[1]
t_len = len(t)

alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
dic = dict(zip(alpha, num))

if dic[s_final] < dic[t_first]:
    print('Yes')
elif dic[s_final] == dic[t_first] and dic[s_semifinal] < dic[t_second]:
    print('Yes')
elif dic[s_final] == dic[t_first] and dic[s_semifinal] == dic[t_second] and s_len < t_len:
    print('Yes')
else:
    print('No')

Fixed submission:
s = list(input())
t = list(input())

s.sort()
t.sort(reverse=True)

if ''.join(s) < ''.join(t):
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
kind = len(set(a))
kind_num = []

if kind < k:
    print(0)
    exit()

for num in set(a):
    one = a.count(num)
    kind_num.append(one)

kind_num.sort()
ans = sum(kind_num[:-k])
print(ans)

Fixed submission:
from collections import Counter

n, k = map(int, input().split())
ary = Counter(list(map(int, input().split())))

if len(ary.keys()) <= k:
    print(0)
    exit()

print(sum(sorted(ary.values())[:len(ary.keys())-k]))

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
kind = len(set(a))
kind_num = []

if kind < k:
    print(0)
    exit()

for num in set(a):
    one = a.count(num)
    kind_num.append(one)

kind_num.sort()
ans = sum(kind_num[:-k])
print(ans)

Fixed submission:
from collections import Counter

n, k = map(int, input().split())
ary = Counter(list(map(int, input().split())))

if len(ary.keys()) <= k:
    print(0)
    exit()

print(sum(sorted(ary.values())[:len(ary.keys())-k]))
Buggy python submission (verdict: Runtime Error):
s = list(input())
a_index = [i for i, x in enumerate(s) if x == 'A']
z_index = [i for i, x in enumerate(s) if x == 'Z']
distance = []

for i in a_index:
    for j in z_index:
        if i <= j:
            dis = j-i
            distance.append(dis)
        else:
            break

print(max(distance)+1)


Fixed submission:
s = input()

print(s.rfind('Z') - s.find('A') + 1)

Buggy python submission (verdict: Runtime Error):
s = list(input())
a_index = [i for i, x in enumerate(s) if x == 'A']
z_index = [i for i, x in enumerate(s) if x == 'Z']
distance = []

for i in a_index:
    for j in z_index:
        if i <= j:
            dis = abs(i-j)
            distance.append(dis)
        else:
            break

print(max(distance)+1)


Fixed submission:
s = input()

print(s.rfind('Z') - s.find('A') + 1)

Buggy python submission (verdict: Runtime Error):
s = list(input())
a_index = [i for i, x in enumerate(s) if x == 'A']
z_index = [i for i, x in enumerate(s) if x == 'Z']
distance = []

for i in a_index:
    for j in z_index:
        if i < j:
            dis = abs(i-j)
            distance.append(dis)
        else:
            break

print(max(distance)+1)

Fixed submission:
s = input()

print(s.rfind('Z') - s.find('A') + 1)

Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
a_index = [i for i, x in enumerate(s) if x == 'A']
z_index = [i for i, x in enumerate(s) if x == 'Z']
distance = []

for i in a_index:
    for j in z_index:
        dis = abs(i-j)
        distance.append(dis)

print(max(distance)+1)

Fixed submission:
s = input()

print(s.rfind('Z') - s.find('A') + 1)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
import heapq
# import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

X, Y, Z, K = ds_in()
A_list = ds_in()
B_list = ds_in()
C_list = ds_in()

A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)


def search(cand, searched):
    if len(searched) > K-1:
        exit()
    current = searched[-1][1]
    cand1 = (current[0]+1, current[1], current[2])
    cand2 = (current[0], current[1]+1, current[2])
    cand3 = (current[0], current[1], current[2]+1)
    if cand1[0] < len(A_list):
        val = A_list[cand1[0]] + B_list[cand1[1]] + C_list[cand1[2]]
        if (-val, cand1) not in cand:
            heapq.heappush(cand, (-val, cand1))
    if cand2[1] < len(B_list):
        val = A_list[cand2[0]] + B_list[cand2[1]] + C_list[cand2[2]]
        if (-val, cand2) not in cand:
            heapq.heappush(cand, (-val, cand2))
    if cand3[2] < len(C_list):
        val = A_list[cand3[0]] + B_list[cand3[1]] + C_list[cand3[2]]
        if (-val, cand3) not in cand:
            heapq.heappush(cand, (-val, cand3))
    next_ = heapq.heappop(cand)
    print(-next_[0])
    searched.append(next_)
    search(cand=cand, searched=searched)


searched = [(-(A_list[0] + B_list[0] + C_list[0]), (0, 0, 0))]
cand = []
print(-searched[0][0])
search(cand=cand, searched=searched)


Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
import heapq
# import numpy as np
import sys
sys.setrecursionlimit(10**6)


s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

X, Y, Z, K = ds_in()
A_list = ds_in()
B_list = ds_in()
C_list = ds_in()

A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)


def search(cand, searched):
    if len(searched) > K-1:
        exit()
    current = searched[-1][1]
    cand1 = (current[0]+1, current[1], current[2])
    cand2 = (current[0], current[1]+1, current[2])
    cand3 = (current[0], current[1], current[2]+1)
    if cand1[0] < len(A_list):
        val = A_list[cand1[0]] + B_list[cand1[1]] + C_list[cand1[2]]
        if (-val, cand1) not in cand:
            heapq.heappush(cand, (-val, cand1))
    if cand2[1] < len(B_list):
        val = A_list[cand2[0]] + B_list[cand2[1]] + C_list[cand2[2]]
        if (-val, cand2) not in cand:
            heapq.heappush(cand, (-val, cand2))
    if cand3[2] < len(C_list):
        val = A_list[cand3[0]] + B_list[cand3[1]] + C_list[cand3[2]]
        if (-val, cand3) not in cand:
            heapq.heappush(cand, (-val, cand3))
    next_ = heapq.heappop(cand)
    print(-next_[0])
    searched.append(next_)
    # print('cand', cand)
    # print('searched', searched)
    search(cand=cand, searched=searched)


searched = [(-(A_list[0] + B_list[0] + C_list[0]), (0, 0, 0))]
cand = []
print(-searched[0][0])
search(cand=cand, searched=searched)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, Q = ds_in()
S = s_in()
move_list = []
for _ in range(Q):
    t, d = s_in().split()
    move_list.append([t, d])


def disappear(place):
    if place == -1:
        return (False, False)
    else:
        left = False
        right = False
        for i in range(Q):
            current_char = S[place]
            t, d = move_list[i]
            if t == current_char:
                if d == 'R':
                    place += 1
                else:
                    place -= 1
                if place < 0:
                    left = True
                    break
                elif place > N-1:
                    right = True
                    break
        return (left, right)


def bisect_left():
    start, end = -1, N
    while end - start > 1:
        t = start + (end-start)//2
        if disappear(t)[0]:
            start = t
        else:
            end = t
    return start


def bisect_right():
    start, end = -1, N
    while end - start > 1:
        t = start + (end-start)//2
        if disappear(t)[1]:
            end = t
        else:
            start = t
    return start


left = bisect_left()
right = bisect_right()
print(right - left)


Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, Q = ds_in()
S = s_in()
move_list = []
for _ in range(Q):
    t, d = s_in().split()
    move_list.append([t, d])


def disappear(place, array):
    if place == -1:
        return (False, False)
    else:
        left = False
        right = False
        for i in range(Q):
            current_char = array[place]
            t, d = move_list[i]
            if t == current_char:
                if d == 'R':
                    place += 1
                else:
                    place -= 1
                if place < 0:
                    left = True
                    break
                elif place > N-1:
                    right = True
                    break
        return (left, right)


def bisect_left(array):
    start, end = -1, N
    while end - start > 1:
        t = start + (end-start)//2
        if disappear(t, array)[0]:
            start = t
        else:
            end = t
    return start


def bisect_right(array):
    start, end = -1, N
    while end - start > 1:
        t = start + (end-start)//2
        if disappear(t, array)[1]:
            end = t
        else:
            start = t
    return start


list_s = list(S)
left = bisect_left(list_s)
right = bisect_right(list_s)
print(right - left)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
from collections import Counter
import numpy as np


s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, K = ds_in()
t_list = []
d_list = []
for _ in range(N):
    t, d = ds_in()
    t_list.append(t)
    d_list.append(d)
np_t = np.array(t_list)
np_d = np.array(d_list)

sort_idx = np.argsort(np_d)[::-1][:K]
top_sushi = np_t[sort_idx]
top_score = np_d[sort_idx]
kind = len(set(top_sushi))
point = top_score.sum() + kind**2
point_list = [point]
sushi_counter = Counter(top_sushi)

sushi_set = {k: [] for k in set(t_list)}
for i, j in zip(t_list, d_list):
    sushi_set[i].append(j)
max_sushi = {k: max(sushi_set[k]) for k in set(t_list)}

for s in set(top_sushi):
    max_sushi.pop(s)
max_values = list(max_sushi.values())
max_values.sort(reverse=True)

for sushi, score in zip(top_sushi[::-1], top_score[::-1]):
    if sushi_counter[sushi] > 1:
        sushi_counter[sushi] -= 1
        kind += 1
        point = point - score + max_values.pop(0) + 2*kind - 1
        point_list.append(point)
        if len(max_values) == 0:
            break

print(max(point_list))

Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
from collections import Counter
import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, K = ds_in()
t_list = []
d_list = []
for _ in range(N):
    t, d = ds_in()
    t_list.append(t)
    d_list.append(d)
np_t = np.array(t_list)  # sushi kind
np_d = np.array(d_list)  # sushi value

sort_idx = np.argsort(np_d)[::-1][:K]  # top K value sushi
top_sushi = np_t[sort_idx]
top_score = np_d[sort_idx]
kind = len(set(top_sushi))
point = top_score.sum() + kind**2  # temp max point
point_list = [point]
sushi_counter = Counter(top_sushi)

sushi_set = {k: [] for k in set(t_list)}
for i, j in zip(t_list, d_list):
    sushi_set[i].append(j)
max_sushi = {k: max(sushi_set[k]) for k in set(t_list)}

for s in set(top_sushi):
    max_sushi.pop(s)  # temp max point
max_values = list(max_sushi.values())
max_values.sort(reverse=True)
if len(max_values) > 0:
    for sushi, score in zip(top_sushi[::-1], top_score[::-1]):
        if sushi_counter[sushi] > 1:
            sushi_counter[sushi] -= 1
            kind += 1
            point = point - score + max_values.pop(0) + 2*kind - 1
            point_list.append(point)
            if len(max_values) == 0:
                break

print(max(point_list))
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
# from bisect import bisect_left
# import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 10**9 + 7


def check_length(list_):
    return len(list_) > len(set(list_))


N, M = ds_in()
A_list = ds_in()
B_list = ds_in()
if check_length(A_list) or check_length(B_list):
    print(0)
    exit()

A_list.sort()
B_list.sort()

NM = N * M
ans = 1
a_idx = 0
b_idx = 0
for i in range(1, NM+1):
    flag = 1
    if i == A_list[a_idx]:
        n = 1
        a_idx += 1
        flag = 0
    else:
        n = N - a_idx

    if i == B_list[b_idx]:
        m = 1
        b_idx += 1
        flag = 0
    else:
        m = M - b_idx

    ans *= (n * m - flag * (NM - i))
    ans = ans % MOD

print(ans)


Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
# from bisect import bisect_left
# import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 10**9 + 7


def check_length(list_):
    return len(list_) > len(set(list_))


N, M = ds_in()
NM = N * M
A_list = ds_in()
B_list = ds_in()
if check_length(A_list) or check_length(B_list):
    print(0)
    exit()

A_list.sort()
B_list.sort()

if A_list[-1] < NM or B_list[-1] < NM:
    print(0)
    exit()

ans = 1
a_idx = 0
b_idx = 0
for i in range(1, NM+1):
    flag = 1
    if i == A_list[a_idx]:
        n = 1
        a_idx += 1
        flag = 0
    else:
        n = N - a_idx

    if i == B_list[b_idx]:
        m = 1
        b_idx += 1
        flag = 0
    else:
        m = M - b_idx

    ans *= (n * m - flag * (NM - i))
    ans = ans % MOD

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1] # s = s_in()
d_in = lambda: int(stdin.readline()) # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split())) # List = ds_in()

N, M = ds_in()
S = s_in()
T = s_in()

# 最大公約数
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a


# 最小公倍数
def lcm(a, b):
	return a * b // gcd (a, b)

lcm_NM = lcm(N, M)
for i in range(N * M // lcm_NM):
    if S[i*lcm_NM//M] != T[i*lcm_NM//N]:
        print(-1)
        exit()

ans = ''
for i in range(lcm_NM):
    if i % lcm_NM == 0:
        ans += S[i // M]
    elif i % M == 0:
        ans += S[i // M]
    elif i % N == 0:
        ans += T[i // N]
    else:
        ans += 'a'
print(len(ans))



Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1] # s = s_in()
d_in = lambda: int(stdin.readline()) # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split())) # List = ds_in()

N, M = ds_in()
S = s_in()
T = s_in()

# 最大公約数
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a


# 最小公倍数
def lcm(a, b):
	return a * b // gcd (a, b)

lcm_NM = lcm(N, M)
for i in range(N * M // lcm_NM):
    if S[i*lcm_NM//M] != T[i*lcm_NM//N]:
        print(-1)
        exit()

print(lcm_NM)