Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03402 (python)

Fixed submission:
#!/usr/bin/env python3
import sys


def solve(A: int, B: int):
    # 100,100のグリッド作る
    grid = [["."]*100 for _ in range(50)]+ [["#"]*100 for _ in range(50)]
    A-=1
    B-=1

    # .を＃に置き換えていく
    for i in range(50):
        for j in range(100):
            if i%2 == 0 and j%2 == 0 and B > 0:
                grid[i][j] = "#"
                B-=1
    
    for i in range(51,100):
        for j in range(100):
            if i%2 == 0 and j%2 == 0 and A>0:
                grid[i][j] = "."
                A-=1
    
    print(100, 100)
    for i in range(100):
        print(''.join(grid[i]))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    solve(A, B)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


def solve(A: int, B: int):
    # 100,100のグリッド作る
    grid = [["."]*100 for _ in range(50)]+ [["#"]*100 for _ in range(50)]
    A-=1
    B-=1

    # .を＃に置き換えていく
    for i in range(50):
        for j in range(100):
            if (i%2)^(j%2) == 1 and B > 0:
                grid[i][j] = "#"
                B-=1
    
    for i in range(51,100):
        for j in range(100):
            if (i%2)^(j%2) == 1 and A>0:
                grid[i][j] = "."
                A-=1
    
    print(100,100)
    for i in range(100):
        print(''.join(rid[i]))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    solve(A, B)

if __name__ == '__main__':
    main()
Problem p03409 (python)

Fixed submission:
n = int(input())
lired = [list(map(int,input().split())) for _ in range(n)]
liblue = [list(map(int,input().split())) for _ in range(n)]
lired = sorted(lired,key = lambda x:-x[1])
liblue = sorted(liblue,key = lambda x:x[0])
li = list(range(n))
count = 0

for i in range(n):
  for j in li:
    if lired[i][0] < liblue[j][0] and lired[i][1] < liblue[j][1]:
      count += 1
      li.remove(j)
      break
print(count)

Buggy submission (verdict: Runtime Error):
n = int(input())
red = [list(map(int,input().split())) for _ in range(n)]
blue = [list(map(int,input().split())) for _ in range(n)]
red_count = []
import copy

for i in range(n):
  index_list = []
  for j in range(n):
    if blue[j][0]>red[i][0] and blue[j][1]>red[i][1]:
      index_list.append(j)
  red_count.append(index_list)
red_count.sort(key=lambda x: len(x))

def redcounter(count,i,removed_index):
  if i == len(red_count)-1 and set(red_count[i])-set(removed_index):
    return count+1
  elif  i == len(red_count)-1: 
    return count
  
  if red_count[i]:
    for index in range(len(red_count[i])): ## removeするindex
      if red_count[i][index] not in removed_index:
        removed_index_copy = copy.copy(removed_index)
        c = count
        removed_index_copy.append(red_count[i][index])
        c = max(c,redcounter(count,i+1,removed_index_copy))
    return c+1
  else:
    return redcounter(count,i+1,removed_index)
   
print(redcounter(0,0,[]))
Problem p03651 (python)

Fixed submission:
#!/usr/bin/env python3
import sys

YES = "POSSIBLE"  # type: str
NO = "IMPOSSIBLE"  # type: str

def solve(N: int, K: int, A: "List[int]"):
    if K in A:
        print(YES)
        return
    
    if max(A) < K:
        print(NO)
        return
    
    ## 互いに素のものが一つでもあれば1が作れるからなんでも作れる→互いにそのものがあるか判定
    import fractions
    from functools import reduce
    def gcd_list(numbers):
        return reduce(fractions.gcd, numbers)
    
    if gcd_list(A) == 1:
        print(YES)
    else:
        if K%gcd_list(A) == 0:
            print(YES)
        else:
            print(NO)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

YES = "POSSIBLE"  # type: str
NO = "IMPOSSIBLE"  # type: str


def solve(N: int, K: int, A: "List[int]"):
    if K in A:
        print(YES)
        return
    
    if max(A) < K:
        print(NO)
        return
    
    ## 互いに素のものが一つでもあれば1が作れるからなんでも作れる→互いにそのものがあるか判定
    from math import fractions
    from functools import reduce
    def gcd_list(numbers):
        return reduce(fractions.gcd, numbers)
    
    if gcd_list(A) == 1:
        print(YES)
    else:
        if K%gcd_list(A) == 0:
            print(YES)
        else:
            print(NO)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()
Problem p04013 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
# dp[j][t]=(y1,...,yj から 0 枚以上選んで yi の合計を t − 1250 にするような選び方の総数)

def solve(N: int, A: int, x: "List[int]"):
    x = [x[i]-A for i in range(N)]
    dp = [[0]*2500 for _ in range(N+1)]
    dp[0][1250] = 1

    for j in range(1,N+1):
        for t in range(2500):
            if t-x[j-1]>=0 and t-x[j-1]<2500:
                dp[j][t] = dp[j-1][t] + dp[j-1][t-x[j-1]]
            else:
                dp[j][t] = dp[j-1][t]
    print(dp[N][1250]-1)
    
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, x)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

def solve(N: int, A: int, x: "List[int]"):
    ## dp[i][j][k] = i番目までのカードからjマイ選んで合計kにする選び方の総数
    dp = [[[0]*2500 for _ in range(N+1)] for _ in range(N+1)]

    for i in range(N):
        dp[i][0][0] = 1

    for i in range(N):
        for j in range(N):
            for k in range(2500):
                if k-x[i] >= 0:
                    dp[i+1][j+1][k] = dp[i][j][k-x[i]]+dp[i][j+1][k]
                else:
                    dp[i+1][j+1][k] = dp[i][j+1][k]

    answer = 0
    for j in range(1,N+1):
        answer += dp[N][j][A*j]
    print(answer)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, x)

if __name__ == '__main__':
    main()
Problem p02664 (python)

Fixed submission:
import numpy as np
T=[]
T=list(input())
values = np.array(T)
searchval = '?'
ii = np.where(values == searchval)[0]

if len(ii)>0:
    for k in ii:
        if k==0:
            if len(T)>1:
                if T[k+1]=='D':
                    T[k]='P'
                else:
                    T[k]='D'
            else:
                T[k] = 'D'

        else:
            if T[k-1] == 'P':
                T[k] = 'D'
            else:
                if k+1<len(T):
                    if T[k+1] == 'D':
                        T[k] = 'P'
                    else:
                        T[k]='D'
                else:
                    T[k] = 'D'

newT=''.join(map(str,T))
print(newT)

Buggy submission (verdict: Runtime Error):
import numpy as np
T=[]
T=list(input())
values = np.array(T)
searchval = '?'
ii = np.where(values == searchval)[0]

if len(ii)>0:
    for k in ii:
        if k==0:
            if T[k+1]=='D':
                T[k]='P'
            else:
                T[k]='D'

        else:
            if T[k-1] == 'P':
                T[k] = 'D'
            else:
                T[k] = 'P'

newT=''.join(map(str,T))
print(newT)
Problem p03264 (python)

Fixed submission:
k = int(input())
ans = (k/2)**2 if k%2==0 else (k+1)/2 * ((k+1)/2 -1)
print(int(ans))

Buggy submission (verdict: Runtime Error):
k = int(input())
ans = k%2 == 0 ? (k/2)**2 : (k+1)/2 * ((k+1)/2 - 1)
print(ans)

Buggy submission (verdict: Runtime Error):
k = int(input())
ans = k%2 == 0 ? (k/2)**2 : (k//2)*(k//2 -1)
print(ans)
Problem p03281 (python)

Fixed submission:
import math

n = int(input())

cnt = 0

def make_prime_list_2(num):
    if num < 2:
        return []

    # 0のものは素数じゃないとする
    prime_list = [i for i in range(num + 1)]
    prime_list[1] = 0 # 1は素数ではない
    num_sqrt = math.sqrt(num)

    for prime in prime_list:
        if prime == 0:
            continue
        if prime > num_sqrt:
            break

        for non_prime in range(2 * prime, num, prime):
            prime_list[non_prime] = 0

    return [prime for prime in prime_list if prime != 0]

def search_divisor_num_1(num):
    if num < 0:
        return None
    elif num == 1:
        return 1
    else:
        num_sqrt = math.floor(math.sqrt(num))
        prime_list = make_prime_list_2(num_sqrt)

        divisor_num = 1
        for prime in prime_list:
            count = 1
            while num % prime == 0:
                num //= prime
                count += 1
            divisor_num *= count

        if num != 1:
            divisor_num *= 2

        return divisor_num

for i in range(n+1):
    if i%2==1 and search_divisor_num_1(i) == 8:
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
import math

n = int(input())

cnt = 0
def search_divisor_num_1(num):
    if num < 0:
        return None
    elif num == 1:
        return 1
    else:
        num_sqrt = math.floor(math.sqrt(num))
        prime_list = make_prime_list_2(num_sqrt)

        divisor_num = 1
        for prime in prime_list:
            count = 1
            while num % prime == 0:
                num //= prime
                count += 1
            divisor_num *= count

        if num != 1:
            divisor_num *= 2

        return divisor_num

for i in range(n+1):
    if i%2==0 and search_divisor_num_1(i) == 8:
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())

cnt = 0
def search_divisor_num_1(num):
    if num < 0:
        return None
    elif num == 1:
        return 1
    else:
        num_sqrt = math.floor(math.sqrt(num))
        prime_list = make_prime_list_2(num_sqrt)

        divisor_num = 1
        for prime in prime_list:
            count = 1
            while num % prime == 0:
                num //= prime
                count += 1
            divisor_num *= count

        if num != 1:
            divisor_num *= 2

        return divisor_num

for i in range(n+1):
    if i%2==0 and search_divisor_num_1(i) == 8:
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())

cnt = 0
def search_divisor_num_1(num):
    if num < 0:
        return None
    elif num == 1:
        return 1
    else:
        num_sqrt = math.floor(math.sqrt(num))
        prime_list = make_prime_list_2(num_sqrt)

        divisor_num = 1
        for prime in prime_list:
            count = 1
            while num % prime == 0:
                num //= prime
                count += 1
            divisor_num *= count

        if num != 1:
            divisor_num *= 2

        return divisor_num

for i in range(n):
    if (i+1)%2==0 and search_divisor_num_1(i+1) == 8:
        cnt += 1

print(cnt)
Problem p03289 (python)

Fixed submission:
s = list(input())
l = len(s)
if s[0] == 'A':
    c_cnt = 0
    cn_l = []
    for i, m in enumerate(s):
        if 1<i<l-1 and m=='C':
            cn_l.append(i)
            c_cnt += 1
    if c_cnt == 1:
        cn = int(cn_l[0])
        tmp = s
        tmp.pop(0)
        tmp.remove('C')
        tmp_s = ''.join(tmp)
        if tmp_s == tmp_s.lower():
            print('AC')
        else:
            print('WA')
    else:
        print('WA')
else:
    print('WA')

Buggy submission (verdict: Runtime Error):
s = list(input())
l = len(s)
if s[0] == 'A':
    c_cnt = 0
    cn_l = []
    for i, m in enumerate(s):
        if 1<i<l-1 & m=='C':
            cn_l.append(i)
            c_cnt += 1
    if c_cnt == 1:
        cn = cn_l[0]
        tmp = s
        tmp.pop(0)
        tmp.pop(cn)
        tmp_s = ''.join(tmp)
        if tmp_s == tmp_s.lower():
            print('AC')
else:
    print('WA')

Buggy submission (verdict: Runtime Error):
s = list(input())
l = len(s)
if s[0] == 'A':
    c_cnt = 0
    for i, m in enumerate(s):
        if 1<i<l-1 & m=='C':
            cn = i
            c_cnt += 1
    if c_cnt == 1:
        tmp = s
        tmp.pop(0)
        tmp.pop(cn)
        tmp_s = ''.join(tmp)
        if tmp_s == tmp_s.lower():
            print('AC')
else:
    print('WA')
Problem p03316 (python)

Fixed submission:
n = int(input())
l = list(str(n))
s = 0
for i in l:
    s += int(i)
if n>=s and n%s == 0:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
n , k = map(int, input().split())
print(n//(k-1))
Problem p03425 (python)

Fixed submission:
N = int(input())
member = [0,0,0,0,0]
ans = 0
for _ in range(N):
    a = list(input())
    if a[0] == "M":
        member[0] += 1
    elif a[0] == "A":
        member[1] += 1
    elif a[0] == "R":
        member[2] += 1
    elif a[0] == "C":
        member[3] += 1
    elif a[0] == "H":
        member[4] += 1
 
for i in range(3):
    for j in range(i+1, 4):
        for k in range(j+1, 5):
            ans += member[i]*member[j]*member[k]

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
member = [0,0,0,0,0]
ans = 0
for _ in range(N):
    a = input().split()
    if a[0] == "M":
        member[0] += 1
    elif a[1] == "A":
        member[1] += 1
    elif a[2] == "R":
        member[2] += 1
    elif a[3] == "C":
        member[3] += 1
    elif a[4] == "H":
        member[4] += 1
 
for i in range(3):
    for j in range(i+1, 4):
        for k in range(j+2, 5):
            ans += member[i]*member[j]*member[k]

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
member = [0,0,0,0,0]
ans = 0
for _ in range(N):
    a = input().split()
    if a[0] == "M":
        member[0] += 1
    elif a[1] == "A":
        member[1] += 1
    elif a[2] == "R":
        member[2] += 1
    elif a[3] == "C":
        member[3] += 1
    elif a[4] == "H":
        member[4] += 1
 
for i in range(3):
    for j in range(i+1, 4):
        for k in range(i+2, 5):
            ans += member[i]*member[j]*member[k]

print(ans)
Problem p03434 (python)

Fixed submission:
N = int(input())
cards = sorted(map(int, input().split()), reverse=True)
A, B = sum(cards[0::2]), sum(cards[1::2])
print(str(A-B))

Buggy submission (verdict: Runtime Error):
N = int(input())
cards = map(int, input().split()).sort(reverse=True)
A, B = sum(cards[0::2]), sum(cards[1::2])
print(str(A-B))
Problem p03449 (python)

Fixed submission:
n = int(input())
A = []
for _ in range(2):
    A.append(list(map(int, input().split())))
cand = []
for i in range(n):
    cand.append(sum(A[0][:i+1])+sum(A[1][i:]))
print(max(cand))

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())
c = int(input())
x = int(input())
ans = 0
# 全探索
for i in range(a+1):
    for j in range(b+1):
        for k in range(c+1):
            tmp = i*500 + j*100 + k*50
            if tmp == x:
                ans += 1
print(ans)
Problem p03502 (python)

Fixed submission:
n = int(input())
l = list(str(n))
f = 0
for i in l:
  f += int(i)
if n%f == 0:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
n = int(input())
l = list(n)
f = 0
for i in l:
  f += int(i)
if n%f == 0:
  print('Yes')
else:
  print('No')
Problem p03543 (python)

Fixed submission:
num = [int(x) for x in list(input())]

if num[0] == num[1] and num[1] == num[2]:
    print("Yes")
elif num[1] == num[2] and num[2] == num[3]:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
num = input().split()
if num[0] == num[1] and num[1] == num[2]:
    print("Yes")
elif num[1] == num[2] and num[2] == num[3]:
    print("Yes")
else:
    print("No")
Problem p03563 (python)

Fixed submission:
R=int(input())
G=int(input())
P = 2*G-R
print(P)

Buggy submission (verdict: Runtime Error):
R=int(input())
G=int(input())
P=2G-R
print(P)

Buggy submission (verdict: Runtime Error):
R=int(input()), G=int(input())
P=2G-R
print(P)
Problem p03573 (python)

Fixed submission:
a,b,c = map(int, input().split())
if a==b:
    print(c)
elif b==c:
    print(a)
else:
    print(b)

Buggy submission (verdict: Runtime Error):
a,b,c = int(input().split())
if a==b:
    print(c)
elif b==c:
    print(a)
else:
    print(b)
Problem p03163 (python)

Fixed submission:
import sys
import math
from  collections import defaultdict


# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

def solve(test):
	n, w = map(int, input().split())
	val = [None] * n
	wgt = [None] * n
	for i in range(n):
		wgt[i], val[i] = map(int, input().split())
	dp = [[0 for j in range(w + 1)] for i in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, w + 1):
			dp[i][j] = dp[i - 1][j]
			if wgt[i - 1] <= j:
				dp[i][j] = max(dp[i][j], val[i - 1] + dp[i - 1][j - wgt[i - 1]])
	ans = dp[-1][-1]
	print(ans)



if __name__ == "__main__":
	test_cases = 1
	for t in range(1, test_cases + 1):
		solve(t)

Buggy submission (verdict: Runtime Error):
import sys
import math
from  collections import defaultdict

sys.setrecurisonlimit(1000000000)

# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

def solve(test):
	n, w = map(int, input().split())
	val = [None] * n
	wgt = [None] * n
	for i in range(n):
		wgt[i], val[i] = map(int, input().split())
	dp = {}
	ans = knapsack(wgt, val, 0, n, dp, w)
	print(ans)

def knapsack(wgt, val, i, n, dp, w):
	if (i, w) in dp:
		return dp[(i, w)]
	elif i >= n:
		return 0
	else:
		dp[(i, w)] = knapsack(wgt, val, i + 1, n, dp, w)
		if wgt[i] <= w:
			dp[(i, w)] = max(dp[(i, w)], val[i] + knapsack(wgt, val, i + 1, n, dp, w - wgt[i]))
		return dp[(i, w)]


if __name__ == "__main__":
	test_cases = 1
	for t in range(1, test_cases + 1):
		solve(t)
Problem p03165 (python)

Fixed submission:
import sys
import math
from  collections import defaultdict

# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')


def solve(test):
	s = [''] + list(input())
	t = [''] + list(input())
	rec = [[0 for j in range(len(t))] for i in range(len(s))]
	for i in range(1, len(s)):
		for j in range(1, len(t)):
			if s[i] == t[j]:
				rec[i][j] = 1 + rec[i - 1][j - 1]
			else:
				rec[i][j] = max(rec[i - 1][j], rec[i][j - 1])

	ans = []
	i, j = len(s) - 1, len(t) - 1
	while i >= 0 and j >= 0:
		if s[i] == t[j]:
			ans.append(s[i])
			i -= 1
			j -= 1
		elif rec[i - 1][j] > rec[i][j - 1]:
			i -= 1
		else:
			j -= 1

	print(''.join(ans[::-1]))


if __name__ == "__main__":
	test_cases = 1
	for t in range(1, test_cases + 1):
		solve(t)

Buggy submission (verdict: Runtime Error):
import sys
import math
from  collections import defaultdict

# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

def solve(test):
	s = input()
	t = input()
	memo = {}
	ans = lcs(s, t, 0, 0, memo)
	print(ans)

def lcs(s, t, i, j, memo):
	if (i, j) in memo:
		return memo[(i, j)]
	elif i == len(s) or j == len(t):
		return ''
	else:
		ans = ''
		if s[i] == t[j]:
			ans = s[i] + lcs(s, t, i + 1, j + 1, memo)
		else:
			s1 = lcs(s, t, i + 1, j, memo)
			s2 = lcs(s, t, i, j + 1, memo)
			if len(s1) >= len(s2):
				ans = s1
			else:
				ans = s2
		memo[(i, j)] = ans
		return ans


if __name__ == "__main__":
	test_cases = 1
	for t in range(1, test_cases + 1):
		solve(t)
Problem p02787 (python)

Fixed submission:
INT_MAX = 10**8
h, n = map(int, input().split())
e = tuple(tuple(map(int, input().split())) for _ in range(n))

dp = [INT_MAX] * (h+1)
dp[0] = 0
for i in range(1, h+1):
  for a, b in e:
    c = dp[max(0, i - a)] + b
    dp[i] = min(dp[i], c)

print(dp[h])

Buggy submission (verdict: Runtime Error):
INT_MAX = 10**8
h, n = map(int, input().split())
e = tuple(tuple(map(int, input().split())))

dp = [0] + [INT_MAX]* h
for i in range(1, h+1):
  v = INT_MAX
  for a, b in e:
    c = dp[max(0, i - a)] + b
    v = min(v, c)
  dp[i] = v
print(dp[h])

Buggy submission (verdict: Runtime Error):
INT_MAX = 10**8
h, n = map(int, input().split())
e = tuple(tuple(map(int, input().split())))

dp = [0] + [INT_MAX]* h
for i in range(1, h+1):
  for a, b in e:
    c = dp[max(0, i - a)] + b
    dp[i] = min(dp[i], c)

print(dp[h])
Problem p02804 (python)

Fixed submission:
from math import factorial
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**5
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  l = len(a)
  for i in range(l):
    s += a[i] * cmb(i, k - 1, mod)
    s -= a[i] * cmb(l - i -1 , k - 1, mod)

    s %= mod
  print(s)
main()

Buggy submission (verdict: Runtime Error):
from math import factorial
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  l = len(a)
  for i in range(l):
    s += a[i] * cmb(i, k - 1, mod)
    s -= a[i] * cmb(l - i -1 , k - 1, mod)

    s %= mod
  print(s)
main()

Buggy submission (verdict: Runtime Error):
from math import factorial
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  if k == 1:
    print(0)
    return
  for i in range(len(a)):
    for j in range(k - 1, len(a) - i):
      s += (a[i+j] - a[i]) * cmb(j-1, k-2, mod) % mod
      s %= mod
  print(s)
main()

Buggy submission (verdict: Runtime Error):
from math import factorial
def comb(n, r):
    return int(factorial(n) / factorial(r) / factorial(n - r))

n, k = map(int, input().split())
a = sorted(map(int, input().split()))

s = 0
d = 10**9+7
for i in range(len(a)):
  for j in range(k - 1, len(a) - i):
    s += (a[i+j] - a[i]) * comb(j-1, k-2) % d
    s %= d
print(s)