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

Fixed submission:
N,K = raw_input().split()
N=int(N)
K=int(K)
if N==1:
    print K
else:
    A = K*pow(K-1,N-1)
    print A

Buggy submission (verdict: Runtime Error):
K = raw_input().split()
N=int(N)
K=int(K)
if N==1:
    print K
else:
    A = K*pow(K-1,N-1)
    print A

Buggy submission (verdict: Runtime Error):

N,K = raw_input().split()
N=int(N)
K=int(K)
if N==1:
    print K
else:
    A = K*pow(K-1,N-1)
    print A

Buggy submission (verdict: Runtime Error):

N,K = raw_input().split()
N=int(N)
K=int(K)
if N==1:
    print K
else:
    A = K*pow(K-1,N-1)
    print A
Problem p02765 (python)

Fixed submission:
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    n, r = map(int, input().split())
    if n>= 10:
        print(r)
    else:
        # r = 100 * (10-n)
        print(r+ 100*(10-n))
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
22 3051
Problem p02789 (python)

Fixed submission:
n, m = map(int, input().split())

if n == m:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from functools import reduce
from fractions import gcd

def lcm_base(x, y):
    return (x * y) // gcd(x, y)

def lcm_list(l):
    lcm = l[0]
    for i in range(len(l)):
        lcm = lcm*l[i] // gcd(lcm, l[i])
    return lcm

mod = 10**9 + 7
n = int(input())
As = list(map(int, input().split()))

lcm = lcm_list(As)
b = [lcm//As[i] for i in range(n)]

print(sum(b) % mod)
Problem p02796 (python)

Fixed submission:
from operator import itemgetter

n = int(input())
XLs = [list(map(int, input().split())) for i in range(n)]

for i in range(n):
    x = XLs[i][0]
    l = XLs[i][1]
    XLs[i].append(x+l)

#右手の端の位置でソート

XLs.sort(key=itemgetter(2))

choice = -10**10
cnt = 0
for i in range(n):
    x = XLs[i][0]
    l = XLs[i][1]

    if choice <= x-l:
        choice = x+l
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
n, k, s = map(int, input().split())

for i in range(1, n+1):
    if i <= k:
        print(s)
    else:
        if s == 10**9:
            print(1)
        else:
            print(10**9)
Problem p02801 (python)

Fixed submission:
c = input()
ordc = ord(c)

if ordc != 122:
    print(chr(int(ordc)+1))
else:
    print('a')

Buggy submission (verdict: Runtime Error):
from collections import deque
import numpy as np
import math

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

def bfs(sy, sx):
    visited = [[-1]*w for i in range(h)]
    visited[sy][sx] = 0
    que = deque([[sy, sx]])
    while que:
        
        y,x = que.popleft()
        for i, j in [(1,0),(0,1),(-1,0),(0,-1)]:
            next_y, next_x = y+i, x+j
            # print(visited)
            if next_y < 0 or next_x < 0 or next_y > h-1 or next_x > w-1:
                continue
            if SHWs[next_y][next_x] == '.' and (visited[next_y][next_x] == -1 or visited[next_y][next_x] > visited[y][x]+1):
                visited[next_y][next_x] = visited[y][x] + 1
                que.append([next_y, next_x])
    
    return np.amax(np.array(visited))

            
ans = 0
for sy in range(h):
    for sx in range(w):
        if SHWs[sy][sx] == '#':
            continue
        count = bfs(sy=sy, sx=sx)

        if count > ans:
            ans = count


print(ans)
Problem p02843 (python)

Fixed submission:
import math
x = int(input())
c = math.floor(x / 100)
if 100*c <= x <= 105*c:
    print(1)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
import math
x = int(input())
c = math.floor(x / 105)
if 100*c <= x 105*c:
    print(1)
else:
    print(0)
Problem p02900 (python)

Fixed submission:
from fractions import gcd
import collections

a, b = map(int, input().split())

gcd = gcd(a, b)


def prime_factorize(n):
    a = list()
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


c = collections.Counter(prime_factorize(gcd))
print(len(c.keys()) + 1)

Buggy submission (verdict: Runtime Error):
import math
import collections

a, b = map(int, input().split())

gcd = math.gcd(a, b)


def prime_factorize(n):
    a = list()
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


c = collections.Counter(prime_factorize(gcd))
print(len(c.keys()) + 1)

Buggy submission (verdict: Runtime Error):
import math
import collections

a, b = map(int, input().split())

gcd = math.gcd(a, b)


def prime_factorize(n):
    a = list()
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


c = collections.Counter(prime_factorize(gcd))
print(len(c.keys()) + 1)

Buggy submission (verdict: Runtime Error):
import math
import collections

a, b = map(int, input().split())

gcd = math.gcd(a, b)


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


c = collections.Counter(prime_factorize(gcd))
print(len(c.keys()) + 1 )
Problem p02923 (python)

Fixed submission:
def main():
    n = int(input())
    h = list(map(int, input().split()))
    # 増加してるマスのあたりをつけてあたりとあたりの距離で勝負する
    check_point = [0]
    if len(h) == 1:
        return 0
    for i in range(len(h)):
        if i == 0:
            continue
        if h[i - 1] < h[i]:
            check_point.append(i)
    if len(check_point) == 1:
        return n-1
    diffs = list()
    if check_point[-1] != n - 1:
        check_point.append(n - 1)
    for i in range(len(check_point)):
        if i == 0:
            continue
        if check_point[i] == n - 1 and h[n - 1] <= h[n - 2]:
            ans = check_point[i] - check_point[i - 1]
        else:
            ans = check_point[i] - 1 - check_point[i - 1]
        diffs.append(ans)
    return max(diffs)

# 0 ~ ci-1 間、 c1  ~ c2-1 間、 c2 ~ end間の距離のうちの最大値を求めてみる

answer = main()
print(answer)

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))
# 増加してるマスのあたりをつけてあたりとあたりの距離で勝負する
check_point = [0]

for i in range(len(h)):
    if i == 0:
        continue
    if h[i-1] < h[i]:
        check_point.append(i)
if len(check_point) == 1:
    print(n-1)
diffs = list()
if check_point[-1] != n-1:
    check_point.append(n-1)
for i in range(len(check_point)):
    if i == 0:
        continue
    if check_point[i] == n-1 and h[n-1] <= h[n-2]:
        ans = check_point[i] - check_point[i - 1]
    else:
        ans = check_point[i] - 1 - check_point[i-1]
    diffs.append(ans)
print(max(diffs))
Problem p02934 (python)

Fixed submission:
n = int(input())
AS = list(map(int, input().split()))

for i in range(n):
    AS[i] = 1 / AS[i]
ans = 1 / sum(AS)
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
as = list(map(int, input().split()))

for i in range(n):
    as[i] = 1 / as[i]
ans = 1 / sum(as)
Problem p03071 (python)

Fixed submission:
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def eprint(s):
    if type(s) != type('str'):
        s=str(s)

    sys.stderr.write(s+'\n')
    return

def main():
    a,b = map(int, input().split())

    cnt = 0
    if a > b:
        cnt += a
        a -= 1
    else:
        cnt += b
        b -= 1
    if a > b:
        cnt += a
        a -= 1
    else:
        cnt += b
        b -= 1
    print(cnt)
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
a,b = map(int, input().split())

cnt = 0

if a > b:
	cnt += a
    a -= 1
else:
	cnt += b
    b -= 1

if a > b:
	cnt += a
    a -= 1
else:
	cnt += b
    b -= 1
print(cnt)
Problem p03612 (python)

Fixed submission:
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def eprint(s):
    if type(s) != type('str'):
        s=str(s)

    sys.stderr.write(s+'\n')
    return

def check(h):
    n = len(h)
    cps = [False]*n
    for i in range(n):
        if h[i] != i+1:
            cps[i] = True
    return cps
        

def main():
    n = int(input())
    Ps = list(map(int, input().split()))
    cnt = int()
    #となりあうFalseは絶対にTrueになる
    cps = check(Ps)
    for i in range(n):
        if not cps[i]:
            if i == n-1:
                cps[i], cps[i-1] = cps[i-1], cps[i]
            else:
                cps[i], cps[i+1] = True, True
            cnt += 1
    eprint(cps)
    print(cnt)
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def eprint(s):
    if type(s) != type('str'):
        s=str(s)

    sys.stderr.write(s+'\n')
    return

def check(h):
    n = len(h)
    cps = [False]*n
    for i in range(n):
        if h[i] != i+1:
            cps[i] = True
    return cps
        

def main():
    n = int(input())
    Ps = list(map(int, input().split()))
    cnt = int()
    #となりあうFalseは絶対にTrueになる
    cps = check(Ps)
    for i in range(n):
        if not cps[i]:
            if i = n-1:
                cps[i], cps[i-1] = cps[i-1], cps[i]
            else:
                cps[i], cps[i+1] = True, True
            cnt += 1
    eprint(cps)
    print(cnt)
    return

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

Fixed submission:
a = int(input())
b = int(input())
if (a > b):
  print("GREATER")
elif (a < b):
  print("LESS")
else:
  print("EQUAL")

Buggy submission (verdict: Runtime Error):
a,b = map(int, input().split())
if (a > b):
  print("GREATER")
elif (a < b):
  print("LESS")
else:
  print("EQUAL")
Problem p03795 (python)

Fixed submission:
n = int(input())
print(800*n - (n//15)*200)

Buggy submission (verdict: Runtime Error):
n = int(input())
print(800*x - (n//15)*200)
Problem p02639 (python)

Fixed submission:
a,b,c,d,e = map(int,input().split())

if a == 0 :
  print(1)
elif b == 0 : 
  print(2)
elif c == 0 :
  print(3)
elif d == 0 : 
  print(4)
else :
  print(5)

Buggy submission (verdict: Runtime Error):

def main():
  a = map(int,input().split())
  ans = 0
  for i in range(5):
      ans += a[i]
  print(15 - ans)
main()

Buggy submission (verdict: Runtime Error):
a = list(map(int,input().split()))

cnt = 1
for i in range(a):
  if i == 0:
    print(cnt)
    cnt += 1

Buggy submission (verdict: Runtime Error):
a = list(map(int,input().split()))

cnt = 1
for i in range(a):
  if i == 0:
    print(cnt)
    cnt += 1

Buggy submission (verdict: Runtime Error):
a = map.int(input().split())
print(15 - sum(a))
Problem p02657 (python)

Fixed submission:
def main():
 a,b = map(int,input().split())
 print(a * b)
main()

Buggy submission (verdict: Runtime Error):

def main():
 a,b = int(input().split())
 print(a * b)
main()
Problem p02675 (python)

Fixed submission:
def main():
    n = int(input())
    if n % 10 == 3:
        print('bon')
    elif n % 10 == 0 or n % 10 == 1 or n % 10 == 6 or n % 10 == 8:
        print('pon')
    else:
        print('hon')
main()

Buggy submission (verdict: Runtime Error):
def main():
    n = int(input())
    if n % 10 == 3:
        print('bon')
    elif n % 10 == 0 or n % 10 == 1 or n % 10 == 6 or n % 10 == 8:
        print('pon')
    else:
        print('hon')
main(
Problem p02706 (python)

Fixed submission:
def main():
 n, m = map(int,input().split())
 a = list(map(int,input().split()))
 sum = 0
 for i in a:
  sum += i
 if sum <= n:
  print(n - sum)
 else:
  print(-1)
      
main()

Buggy submission (verdict: Runtime Error):
def main():
 n, m = map(int,input().split())
 a = list(map(int,input().split()))
 sum = sum(a)
 if sum <= n:
  print(n - sum)
 else:
  print(-1)
      
main()
Problem p02743 (python)

Fixed submission:

def main():
 a,b,c = map(int,input().split())
 rhs = c - a - b
 lhs = 4 * a * b
 if rhs > 0  and lhs < rhs ** 2:
  print('Yes')
 else:
  print('No')
main()

Buggy submission (verdict: Runtime Error):

def main():
 a,b,c = map(int,input().split())
 rhs = c - a - b
 lhs = 4 * a * b
 if rhs > 0  and lhs ** 2 < rhs:
  print('Yes')
 else:
  print('No')
main(

Buggy submission (verdict: Runtime Error):
import decimal
def main():
 a,b,c = map(decimal.Decimal,input().split())
 if a ** 0.5 + b ** 0.5 < c ** 0.5:
  print('Yes')
 else:
  print('No')
main()
Problem p02765 (python)

Fixed submission:
def main():
 n, r = map(int,input().split())
 if n >= 10:
     print(r)
 else:
     print(r + 100* (10 - n))
main()

Buggy submission (verdict: Runtime Error):
def main():
 n, r = map(int,input().split())
 if n >= 10:
     print(r)
 else:
     print(r + 100* (10 - k))
main()
Problem p03050 (python)

Fixed submission:
import math


def main():
    n = int(input())
    a = []
    for i in range(1, (int)(math.sqrt(n)) + 1):
        if n % i == 0:
            if i != 1:
                a.append(i - 1)
            if n // i - 1 != 0:
                a.append((n // i) - 1)
    ans = 0
    for i in range(len(a)):
        if a[i] != 1 and (n // a[i] == n % a[i]):
            ans += a[i]
    print(ans)


main()

Buggy submission (verdict: Runtime Error):
import math


def main():
    n = int(input())
    a = []
    for i in range(1, (int)(math.sqrt(n)) + 1):
        if n % i == 0:
            if i != 1:
                a.append(i - 1)
            a.append((n // i) - 1)
    a.sort()
    ans = 0
    if len(a) == 0:
        ans = 0
    else:
        for i in a:
            if i != 1 and (n // i == n % i):
                ans += i
    print(ans)


main()

Buggy submission (verdict: Runtime Error):
import math
def main():
    n = int(input())
    a = []
    for i in range(1,(int)(math.sqrt(n)) + 1):
        if n % i == 0:
            if i != 1:
                a.append(i - 1)
            a.append((n // i) - 1)
    a.sort()
    ans = 0
    for i in a:
        if i != 1 and (n // i == n % i):
            ans += i
    print(ans)
main()

Buggy submission (verdict: Runtime Error):
import math
def main():
    n = int(input())
    a = []
    for i in range(1,(int)(math.sqrt(n)) + 1):
        if n % i == 0:
            if i != 1:
                a.append(i - 1)
            a.append(n // i - 1)
    a.sort()
    print(a)
    ans = 0
    for i in a:
        if i != 1 and n // i == n % i:
            ans += i
    print(ans)
main()

Buggy submission (verdict: Runtime Error):
import math
def main():
    n = int(input())
    a = []
    for i in range(1,(int)(math.sqrt(n)) + 1):
        if n % i == 0:
            if i != 1:
                a.append(i - 1)
            a.append(n // i - 1)
    a.sort()
    print(a)
    ans = 0
    for i in a:
        if i != 1 and n // i == n % i:
            ans += i
    print(ans)
main()
Problem p03059 (python)

Fixed submission:
def main():
    a,b,t = map(int,input().split())
    ans = 0
    for i in range(1,t + 1, 1):
        if i % a == 0:
            ans += b
    print(ans)
main()

Buggy submission (verdict: Runtime Error):
def main():
    a,b,t = map(int,input().split())
    ans = 0
    for i in range(t + 0.51, 1):
        if i % a == 0:
            ans += b
    print(ans)
main()

Buggy submission (verdict: Runtime Error):
def main():
    a,b,t = map(int,input().split())
    ans = 0
    for i in (t + 1):
        if i % a == 0:
            ans += b
    print(ans)
main()
Problem p03197 (python)

Fixed submission:
def main():
    n = int(input())
    a = list([(int(input())) % 2 for i in range(n)])
    ans = sum(a)
    if(ans == 0):
        print('second')
    else:
        print('first')

main()

Buggy submission (verdict: Runtime Error):
def main():
    n = int(input())
    a = list([(int(input())) % 2 for i in range(n)])
    su = sum(a)
    xors = 0
    for i in(a):
        xors ^= i
    if(xors != 0 || sum == n):
        print('first')
    else:
        print('second')

main()

Buggy submission (verdict: Runtime Error):
def main():
    n = int(input())
    a = [int(input) for i in range(n)]
    ans = sum(a)
    if(ans % 2 == 0):
        print('second')
    else:
        print('first')

main()
Problem p03206 (python)

Fixed submission:
def main():
 D = int(input())
 if D == 25:
     print('Christmas')
 elif D == 24:
     print('Christmas Eve')
 elif D == 23:
     print('Christmas Eve Eve')
 else:
     print('Christmas Eve Eve Eve')
main()

Buggy submission (verdict: Runtime Error):
def main():
 D = int(input().split())
 if D == 25:
     print('Christmas')
 elif D == 24:
     print('Christmas Eve')
 elif D == 23:
     print('Christmas Eve Eve')
 else:
     print('Christmas Eve Eve')
main()
Problem p03351 (python)

Fixed submission:
def main():
 a,b,c,d = map(int,input().split())
 if abs(a - c) <= d or (abs(a - b) <= d and abs(b - c) <= d):
     print('Yes')
 else:
     print('No')
main()

Buggy submission (verdict: Runtime Error):
def main():
 a,b,c,d = map(int,input().split())
 if abs(a - c) <= d or abs(a - b) + abs(b - c) <= d:
main()

Buggy submission (verdict: Runtime Error):
def main():
 a,b,c,d = map(int,input().split())
 if abs(a - c) <= d or abs(a - b) + abs(b - c) <= d
main()
Problem p03761 (python)

Fixed submission:
def main():
  n = int(input())
  s = [input() for i in range(n)]
  nums = [1000 for i in range(26)]
  for i in range(26):
      for j in range(n):
          nums[i] = min(nums[i],s[j].count((chr)(97 + i)))

  for i in range(26):
    while nums[i] > 0:
        print((chr)(97 + i),end = "")
        nums[i] -= 1
  print("")

main()

Buggy submission (verdict: Runtime Error):
def main():
  n = int(input())
  s = [input() for i in range(n)]
  nums = [1000 for i in range(26)]
  for i in range(26):
      for j in range(n):
          nums[i] = min(nums[i],s[j].count((chr)(97 + i)))

  for i in range(n):
    while nums[i] > 0:
        print((chr)(97 + i),end = "")
        nums[i] -= 1
  print("")

main()
Problem p02703 (python)

Fixed submission:
N,M,S=map(int,input().split())
S=min(2500,S)

#O(ElogV)
import heapq
def dijkstra_heap(s):
  #始点sから各頂点への最短距離
  d,used={},{}
  for v in vlist:
    d[v]=float("inf")
    used[v]=True
  d[s]=0
  used[s]=False
  
  edgelist = []
  for e in edge[s]:
    heapq.heappush(edgelist,e)
    
  while len(edgelist):
    minedge = heapq.heappop(edgelist)
    #まだ使われてない頂点の中から最小の距離のものを探す
    if not used[minedge[1]]:
      continue
    v = minedge[1]
    d[v] = minedge[0]
    used[v] = False
    for e in edge[v]:      
      if used[e[1]]:
        heapq.heappush(edgelist,(e[0]+d[v],e[1]))
        
  return d

uvab_list=[]
for i in range(M):
  U,V,A,B=map(int,input().split())
  uvab_list.append((U,V,A,B))
  
cd_list=[]
for i in range(N):
  C,D=map(int,input().split())
  cd_list.append((C,D))
  
vlist=[]
for i in range(1,N+1):
  for j in range(2501):
    vlist.append((i,j))

edge={}
for U,V,A,B in uvab_list:
  for j in range(2501):
    if j-A>=0:
      if not (U,j) in edge:
        edge[(U,j)]=[]    
      edge[(U,j)].append((B,(V,j-A)))
      if not (V,j) in edge:
        edge[(V,j)]=[]
      edge[(V,j)].append((B,(U,j-A)))

for i in range(N):
  C,D=cd_list[i]
  for j in range(2501):
    if not (i+1,j) in edge:
      edge[(i+1,j)]=[]    
    jj=min(2500,j+C)    
    edge[(i+1,j)].append((D,(i+1,jj)))
    
n=len(vlist)
w=len(edge)
res=dijkstra_heap((1,S))
#print(res)

for i in range(2,N+1):
  min_i=float("inf")
  for j in range(2501):
    min_i=min(min_i,res[(i,j)])
    
  print(min_i)

Buggy submission (verdict: Runtime Error):
N,M,S=map(int,input().split())

#O(ElogV)
import heapq
def dijkstra_heap(s):
  #始点sから各頂点への最短距離
  #d = [float("inf")] * n  
  #used = [True] * n #True:未確定
  d,used={},{}
  for v in vlist:
    d[v]=float("inf")
    used[v]=True
  d[s]=0
  used[s]=False
  
  edgelist = []
  for e in edge[s]:
    heapq.heappush(edgelist,e)
    
  while len(edgelist):
    minedge = heapq.heappop(edgelist)
    #まだ使われてない頂点の中から最小の距離のものを探す
    if not used[minedge[1]]:
      continue
    v = minedge[1]
    d[v] = minedge[0]
    used[v] = False
    for e in edge[v]:      
      if used[e[1]]:
        heapq.heappush(edgelist,(e[0]+d[v],e[1]))
        
  return d

uvab_list=[]
for i in range(M):
  U,V,A,B=map(int,input().split())
  uvab_list.append((U,V,A,B))
  
cd_list=[]
for i in range(N):
  C,D=map(int,input().split())
  cd_list.append((C,D))
  
vlist=[]
for i in range(1,N+1):
  for j in range(2501):
    vlist.append((i,j))

edge={}
for U,V,A,B in uvab_list:
  for j in range(2501):
    if j-A>=0:
      if not (U,j) in edge:
        edge[(U,j)]=[]    
      edge[(U,j)].append((B,(V,j-A)))
      if not (V,j) in edge:
        edge[(V,j)]=[]
      edge[(V,j)].append((B,(U,j-A)))

for i in range(N):
  C,D=cd_list[i]
  for j in range(2501):
    if not (i+1,j) in edge:
      edge[(i+1,j)]=[]    
    jj=min(2500,j+C)    
    edge[(i+1,j)].append((D,(i+1,jj)))
    
n=len(vlist)
w=len(edge)
res=dijkstra_heap((1,S))
#print(res)

for i in range(2,N+1):
  min_i=float("inf")
  for j in range(2501):
    min_i=min(min_i,res[(i,j)])
    
  print(min_i)
Problem p02804 (python)

Fixed submission:
MOD=10**9+7
N,K=map(int,input().split())
alist=list(map(int,input().split()))
alist.sort()
#print(alist)

inv_table = [0]+[1]
for i in range(2,N+1):
  inv_table.append(-(MOD//i)*inv_table[MOD%i]%MOD)

min_s,max_s=0,0
old_comb,new_comb=0,1
for i in range(K,N+1):
  #print(K,i,new_comb-old_comb)
  min_s+=(new_comb-old_comb)*alist[-1-(i-1)]
  min_s%=MOD
  max_s+=(new_comb-old_comb)*alist[i-1]
  max_s%=MOD
  
  old_comb=new_comb
  new_comb=old_comb*(i+1)*inv_table[i+1-K]%MOD
#print(min_s,max_s)

print((max_s-min_s)%MOD)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
N,K=map(int,input().split())
alist=list(map(int,input().split()))
alist.sort()
#print(alist)

inv_table = [0]+[1]
for i in range(2,K+3):
  inv_table.append(-(MOD//i)*inv_table[MOD%i]%MOD)

min_s,max_s=0,0
old_comb,new_comb=0,1
for i in range(K,N+1):
  #print(K,i,new_comb-old_comb)
  min_s+=(new_comb-old_comb)*alist[-1-(i-1)]
  min_s%=MOD
  max_s+=(new_comb-old_comb)*alist[i-1]
  max_s%=MOD
  
  old_comb=new_comb
  new_comb=old_comb*(i+1)*inv_table[i+1-K]%MOD
#print(min_s,max_s)

print((max_s-min_s)%MOD)
Problem p02822 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
MOD=10**9+7
N=int(input())

tree=[[] for _ in range(N+1)]
for _ in range(N-1):
  A,B=map(int,input().split())
  tree[A].append(B)
  tree[B].append(A)
#print(tree)

def dfs(u,par):
  visited[u]=True
  ret=1
  for v in tree[u]:
    if not visited[v]:
      ret+=dfs(v,u)

  if par>0:
    tree_dic[(u,par)]=ret
  return ret
  
visited=[False]*(N+1)
tree_dic={}
dfs(1,0)
#print(tree_dic)

bumbo=pow(2,N,MOD)
bunshi=(pow(2,N,MOD)-1-N*pow(2,N-1,MOD))%MOD
for x in tree_dic.values():
  term=(pow(2,x,MOD)-1)*(pow(2,N-x,MOD)-1)
  bunshi+=term  
  bunshi%=MOD
#print(bunshi,bumbo)

answer=bunshi*pow(bumbo,MOD-2,MOD)
answer%=MOD
print(answer)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
N=int(input())

tree=[[] for _ in range(N+1)]
for _ in range(N-1):
  A,B=map(int,input().split())
  tree[A].append(B)
  tree[B].append(A)
#print(tree)

def dfs(u,par):
  visited[u]=True
  ret=1
  for v in tree[u]:
    if not visited[v]:
      ret+=dfs(v,u)

  if par>0:
    tree_dic[(u,par)]=ret
  return ret
  
visited=[False]*(N+1)
tree_dic={}
dfs(1,0)
#print(tree_dic)

bumbo=pow(2,N,MOD)
bunshi=(pow(2,N,MOD)-1-N*pow(2,N-1,MOD))%MOD
for x in tree_dic.values():
  term=(pow(2,x,MOD)-1)*(pow(2,N-x,MOD)-1)
  bunshi+=term  
  bunshi%=MOD
#print(bunshi,bumbo)

answer=bunshi*pow(bumbo,MOD-2,MOD)
answer%=MOD
print(answer)