Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(input())
    s = readline()
    s = list(s.decode('UTF-8').rstrip("\n"))
    s.append(s[0])
    s.insert(0, s[-2])

    init = ((1, 1), (0, 1), (1, 0), (0, 0))
    # init = ((0, 0), (0, 1), (1, 0), (1, 1))
    for i in init:
        p = [0] * (N+2)
        p[0] = i[0]
        p[1] = i[1]
        for j in range(1, N+1):
            if s[j] == "o":
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ p[j]
                else:
                    p[j+1] = p[j-1] ^ 1
            else:
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ 1
                else:
                    p[j+1] = p[j-1] ^ p[j]

        if p[0] == p[-2] and p[1] == p[-1]:
            ss = list(map(lambda x: "W" if x == 1 else "S", p[1:N+1]))
            print(''.join(ss))
            sys.exit()
    else:
        print(-1)


main()


Fixed submission:
import sys


def main():
    N = int(input())
    s = list(input())
    s.append(s[0])
    s.insert(0, s[-2])

    init = ((0, 0), (0, 1), (1, 0), (1, 1))
    for i in init:
        p = [0] * (N+2)
        p[0] = i[0]
        p[1] = i[1]
        for j in range(1, N+1):
            if s[j] == "o":
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ 0
                else:
                    p[j+1] = p[j-1] ^ 1
            else:
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ 1
                else:
                    p[j+1] = p[j-1] ^ 0

        if p[0] == p[-2] and p[1] == p[-1]:
            ss = list(map(lambda x: "W" if x == 1 else "S", p[1:N+1]))
            print(''.join(ss))
            sys.exit()
    else:
        print(-1)


main()
Buggy python submission (verdict: Runtime Error):
import itertools

K, S = map(int, input().split())

cnt = 0

K = list(range(K + 1))

for i, j in itertools.product(K, repeat=2):
    if 0 <= S - i - j <= K:
        cnt += 1

print(cnt)


Fixed submission:
K, S = map(int, input().split())

cnt = 0
for i in range(K+1):
    for j in range(K+1):
        if 0 <= S - i - j <= K:
            cnt += 1

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(range(1, N + 1))

    C = []
    ans = 0
    for a, b, in zip(A, B):
        a, b = min(a, b), max(a, b)
        if [a, b] in C:
            ans += 1
        else:
            C.append([a, b])

    print(ans)


main()


Fixed submission:
import collections
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    B = list(range(1, N + 1))

    C = collections.defaultdict(int)
    for a, b, in zip(A, B):
        a, b = min(a, b), max(a, b)
        C[(a, b)] += 1

    D = collections.Counter(C.values())
    print(D[2])


main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(range(1, N + 1))

    C = []
    ans = 0
    for a, b, in zip(A, B):
        a, b = min(a, b), max(a, b)
        if [a, b] in C:
            ans += 1
        else:
            C.append([a, b])

    print(ans)


main()


Fixed submission:
import collections
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    B = list(range(1, N + 1))

    C = collections.defaultdict(int)
    for a, b, in zip(A, B):
        a, b = min(a, b), max(a, b)
        C[(a, b)] += 1

    D = collections.Counter(C.values())
    print(D[2])


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

N,K,*A = map(int, open(0).read().split())
left = 1
right = max(A)
while left != right:
    mid = (left+right) // 2
    temp = 0
    for x in A:
        temp += ceil(x/mid) - 1
        if temp > K:
            left = mid + 1
            break
    else:
        right = mid
for i in range(left-1,left+2):
    temp = 0
    for x in A:
        temp += ceil(x/i) - 1
        if temp > K:
            break
    else:
        print(i)
        break

Fixed submission:
from math import ceil

N,K,*A = map(int, open(0).read().split())
left = 1
right = max(A)
while left != right:
    mid = (left+right) // 2
    temp = 0
    for x in A:
        temp += ceil(x/mid) - 1
        if temp > K:
            left = mid + 1
            break
    else:
        right = mid
for i in range(max(1,left-1),left+2):
    temp = 0
    for x in A:
        temp += ceil(x/i) - 1
        if temp > K:
            break
    else:
        print(i)
        break
Buggy python submission (verdict: Time Limit Exceeded):
def comb(n,r,m):
	if r == 0: return 1
	return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)
def memfact(a):
	temp = 1
	yield temp
	for i in range(1,a+1):
		temp = temp * i % 1000000007
		yield temp

K = int(input())
S = input()
s = len(S)
ans = 0
m = 1000000007
memf = []
for x in memfact(s+K-1):
	memf.append(x)
for i in range(K+1):
	ans = (ans+comb(s+K-i-1,K-i,m)*pow(25,K-i,m)*pow(26,i,m)) % m
print(ans)

Fixed submission:
def comb(n,r,m):
	if r == 0: return 1
	return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)
def memfact(a):
	temp = 1
	yield temp
	for i in range(1,a+1):
		temp = temp * i % 1000000007
		yield temp

K = int(input())
S = input()
s = len(S)
ans = 0
m = 1000000007
memf = []
for x in memfact(s+K-1):
	memf.append(x)
for i in range(K+1):
	ans = (ans+comb(s+K-i-1,K-i,m)*pow(25,K-i,m)*pow(26,i,m)) % m
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def im(a):
    imos = [0] * (3*N+1)
    for i in range(N):
        imos[N+i-min(N,a[i])] += 1
        imos[N+i+min(N,a[i])+1] -= 1
    cs = [0]
    for i in range(1,2*N+1):
        cs.append(cs[i-1]+imos[i-1])
    return cs[N+1:]

N,K,*A = map(int, open(0).read().split())
for i in range(K):
    A = im(A)
print(*A)

Fixed submission:
def im(a):
    imos = [0] * (3*N+1)
    for i in range(N):
        imos[N+i-min(N,a[i])] += 1
        imos[N+i+min(N,a[i])+1] -= 1
    cs = [0]
    for i in range(1,2*N+1):
        cs.append(cs[i-1]+imos[i-1])
    return cs[N+1:]

N,K,*A = map(int, open(0).read().split())
for i in range(K):
    A = im(A)
    if A.count(N) == N:
        break
print(*A)
Buggy python submission (verdict: Time Limit Exceeded):
N,S,*A = map(int, open(0).read().split())
dp = [[0] * (S+1) for _ in range(N+1)]
dp[0][0] = 1
appeared = set([0])
for i in range(1,N+1):
    temp = A[i-1]
    ts = set()
    for x in appeared:
        dp[i][x] += (dp[i-1][x]*2) % 998244353
        if temp + x <= S:
            dp[i][temp+x] += dp[i-1][x]
            if temp + x not in appeared:
                ts.add(temp+x)
    appeared |= ts
print(dp[N][S]%998244353)

Fixed submission:
N,S,*A = map(int, open(0).read().split())
dp = [[0] * (S+1) for _ in range(N+1)]
dp[0][0] = 1
appeared = set([0])
for i in range(1,N+1):
    temp = A[i-1]
    ts = set()
    for x in appeared:
        dp[i][x] += (dp[i-1][x]*2) % 998244353
        if temp + x <= S:
            dp[i][temp+x] += dp[i-1][x]
            if temp + x not in appeared:
                ts.add(temp+x)
    appeared |= ts
print(dp[N][S]%998244353)

Buggy python submission (verdict: Runtime Error):
import numpy as np

N,S,*A = map(int, open(0).read().split())
b = [x for x in A if x <= S]
coe = np.zeros(S+1,dtype='int64')
coe[0] = 2
coe[b[0]] = 1
for x in b[1:]:
    t1 = np.zeros(S+1,dtype='int64')
    t2 = np.zeros(S+1,dtype='int64')
    for i in range(S+1):
        if i + x <= S:
            t1[i] = (coe[i]*2) % 998244353
            t2[i+x] = coe[i]
        else:
            t1[i] = (coe[i]*2) % 998244353
    coe = t1 + t2
print(coe[S])

Fixed submission:
N,S,*A = map(int, open(0).read().split())
dp = [[0] * (S+1) for _ in range(N+1)]
dp[0][0] = 1
appeared = set([0])
for i in range(1,N+1):
    temp = A[i-1]
    ts = set()
    for x in appeared:
        dp[i][x] += (dp[i-1][x]*2) % 998244353
        if temp + x <= S:
            dp[i][temp+x] += dp[i-1][x]
            if temp + x not in appeared:
                ts.add(temp+x)
    appeared |= ts
print(dp[N][S]%998244353)
Buggy python submission (verdict: Time Limit Exceeded):
import math
K = int(input())
count = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            count += math.gcd(math.gcd(i,j),k)
print(count)

Fixed submission:
import math
K = int(input())
count = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        temp = math.gcd(i,j)
        for k in range(1,K+1):
            count += math.gcd(temp,k)
print(count)
Buggy python submission (verdict: Runtime Error):
from scipy.special import comb
N,M = (int(x) for x in input().split())
a = int(comb(N, 2)) + int(comb(M, 2))
print(a)

Fixed submission:
from scipy.misc import comb
N,M = (int(x) for x in input().split())
a = comb(N, 2, exact=True) + comb(M, 2, exact=True)
print(a)

Buggy python submission (verdict: Runtime Error):
import math
N,M = (int(x) for x in input().split())
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
a = combinations_count(N, 2) + combinations_count(M, 2)
print(a)

Fixed submission:
from scipy.misc import comb
N,M = (int(x) for x in input().split())
a = comb(N, 2, exact=True) + comb(M, 2, exact=True)
print(a)
Buggy python submission (verdict: Runtime Error):
a,b,c = (str(x) for x in input().split())
if a == b == c:
    print('No')
else:
    print('Yes')

Fixed submission:
S = input()
if S[0] == S[1] == S[2]:
    print('No')
else:
    print('Yes')

Buggy python submission (verdict: Runtime Error):
a,b,c = (str(x) for x in input().split())
if a == b == c:
    print("No")
else:
    print("Yes")

Fixed submission:
S = input()
if S[0] == S[1] == S[2]:
    print('No')
else:
    print('Yes')

Buggy python submission (verdict: Runtime Error):
a,b,c = (str(x) for x in input().split())
if a == b == c:
    print(No)
else:
    print(Yes)

Fixed submission:
S = input()
if S[0] == S[1] == S[2]:
    print('No')
else:
    print('Yes')

Buggy python submission (verdict: Runtime Error):
a,b,c = (str(x) for x in input().split())
if a == b == c
    print(No)
    else
        print(Yes)

Fixed submission:
S = input()
if S[0] == S[1] == S[2]:
    print('No')
else:
    print('Yes')
Buggy python submission (verdict: Runtime Error):
A,B = (int(x) for x in input().split())
a = range(math.ceil(A/0.08), math.floor(((A+1)/0.08)+1))
b = range(math.ceil(B/0.1), math.floor(((B+1)/0.1)+1))
c = set(a) & set(b)
if len(c) == 0:
          print(-1)
else:
          print(min(c))

Fixed submission:
import math
A,B = (int(x) for x in input().split())
A1 = math.ceil(A/0.08)
if float((A+1)/0.08).is_integer() == True:
    A2 = math.floor((A+1)/0.08)
else:
    A2 = math.floor((A+1)/0.08) + 1
B1 = math.ceil(B/0.1)
if float((B+1)/0.1).is_integer() == True:
    B2 = math.floor((B+1)/0.1)
else:
    B2 = math.floor((B+1)/0.1) + 1
a = range(A1, A2)
b = range(B1, B2)
c = set(a) & set(b)
if len(c) == 0:
          print(-1)
else:
          print(min(c))
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
s_is_reversed = False
for j in range(Q):
    if Query[j][0] == '1':
        s_is_reversed = not s_is_reversed
    elif Query[j][1] == '1':
        if s_is_reversed == False:
            s.insert(0,Query[j][2])
        else:
            s.append(Query[j][2])
    else:
        if s_is_reversed == False:
            s.append(Query[j][2])
        else:
            s.insert(0,Query[j][2])
if s_is_reversed == True:
    s.reverse()
printed = ''.join(s)
print(printed)

Fixed submission:
from collections import deque
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
d = deque(s)
d_is_reversed = False
for j in range(Q):
    if Query[j][0] == '1':
        d_is_reversed = not d_is_reversed
    elif Query[j][1] == '1':
        if d_is_reversed == False:
            d.appendleft(Query[j][2])
        else:
            d.append(Query[j][2])
    else:
        if d_is_reversed == False:
            d.append(Query[j][2])
        else:
            d.appendleft(Query[j][2])
if d_is_reversed == True:
    d.reverse()
printed = ''.join(d)
print(printed)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
for j in range(Q):
    if Query[j][0] == '1':
        s.reverse()
    elif Query[j][1] == '1':
        s.insert(0,Query[j][2])
    else:
        s.append(Query[j][2])
printed = ''.join(s)
print(printed)

Fixed submission:
from collections import deque
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
d = deque(s)
d_is_reversed = False
for j in range(Q):
    if Query[j][0] == '1':
        d_is_reversed = not d_is_reversed
    elif Query[j][1] == '1':
        if d_is_reversed == False:
            d.appendleft(Query[j][2])
        else:
            d.append(Query[j][2])
    else:
        if d_is_reversed == False:
            d.append(Query[j][2])
        else:
            d.appendleft(Query[j][2])
if d_is_reversed == True:
    d.reverse()
printed = ''.join(d)
print(printed)
Buggy python submission (verdict: Runtime Error):
def memfact(a,m):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % m
        yield temp
def comb(n,r,m):
    if r == 0: return 1
    return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)

n,k = map(int, open(0).read().split())
dp = [0] * n
dp[0] = 1
dp[1] = (n * (n-1)) % m
m = 1000000007
memf = []
mfappend = memf.append
for x in memfact(n,m):
    mfappend(x)
for i in range(2,min(k+1,n)):
    dp[i] = (dp[i-1]+comb(n,i,m)*comb(n-1,i,m)) % m
print((dp[min(n-1,k)]+1)%m)

Fixed submission:
def memfact(a,m):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % m
        yield temp
def comb(n,r,m):
    if r == 0: return 1
    return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)

n,k = map(int, open(0).read().split())
dp = [0] * n
dp[0] = 1
m = 1000000007
dp[1] = (n * (n-1)) % m
memf = []
mfappend = memf.append
for x in memfact(n,m):
    mfappend(x)
for i in range(2,min(k+1,n)):
    dp[i] = (dp[i-1]+comb(n,i,m)*comb(n-1,i,m)) % m
print((dp[min(n-1,k)]+1)%m)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
C = Counter(S)
M = [kv[0] for kv in C.items() if kv[1] == max(C.values())]
M.sort()
A = '\n'.join(M)
print(A)

Fixed submission:
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
C = Counter(S)
freq = max(C.values())
M = [kv[0] for kv in C.items() if kv[1] == freq]
M.sort()
A = '\n'.join(M)
print(A)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
C = Counter(S)
M = [kv[0] for kv in C.items() if kv[1] == max(C.values())]
M.sort()
A = '\n'.join(M)
print(A)

Fixed submission:
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
C = Counter(S)
freq = max(C.values())
M = [kv[0] for kv in C.items() if kv[1] == freq]
M.sort()
A = '\n'.join(M)
print(A)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
C = Counter(S)
M = [kv[0] for kv in C.items() if kv[1] == max(C.values())]
M.sort()
for i in range(len(M)):
    print(M[i])

Fixed submission:
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
C = Counter(S)
freq = max(C.values())
M = [kv[0] for kv in C.items() if kv[1] == freq]
M.sort()
A = '\n'.join(M)
print(A)
Buggy python submission (verdict: Runtime Error):
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = p1.cumsum()
p2 = np.array([(s1[i+K] - s1[i]) for i in range(N-K)])
max = np.amax(p2)
print((max+K) / 2)

Fixed submission:
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = p1.cumsum()
s2 = np.append(0, s1)
p2 = np.array([(s2[i+K] - s2[i]) for i in range(N-K+1)])
max = np.amax(p2)
print((max+K) / 2)

Buggy python submission (verdict: Runtime Error):
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = np.array([np.sum(p1[0:i+1]) for i in range(N)])
p2 = np.array([(s1[i+K] - s1[i]) for i in range(N-K)])
max = np.amax(p2)
print((max+K) / 2)

Fixed submission:
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = p1.cumsum()
s2 = np.append(0, s1)
p2 = np.array([(s2[i+K] - s2[i]) for i in range(N-K+1)])
max = np.amax(p2)
print((max+K) / 2)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
p2 = np.array([np.sum(p1[i:i+K]) for i in range(N-K+1)])
max = np.amax(p2)
print((max+K) / 2)

Fixed submission:
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = p1.cumsum()
s2 = np.append(0, s1)
p2 = np.array([(s2[i+K] - s2[i]) for i in range(N-K+1)])
max = np.amax(p2)
print((max+K) / 2)

Buggy python submission (verdict: Runtime Error):
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
p2 = (p1+1) / 2
p3 = np.array([np.sum(p2[i:i+K]) for i in range(N-K+1)])
print(np.amax(p3))

Fixed submission:
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = p1.cumsum()
s2 = np.append(0, s1)
p2 = np.array([(s2[i+K] - s2[i]) for i in range(N-K+1)])
max = np.amax(p2)
print((max+K) / 2)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
p2 = (p1+1) / 2
p3 = np.array([np.sum(p2[i:i+K]) for i in range(N-K+1)])
print(np.amax(p3))

Fixed submission:
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = p1.cumsum()
s2 = np.append(0, s1)
p2 = np.array([(s2[i+K] - s2[i]) for i in range(N-K+1)])
max = np.amax(p2)
print((max+K) / 2)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000000)
H,N = (int(x) for x in input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
mem = [0] * H
def dp(rem):
    global mem
    if rem <= 0:
        return 0
    elif mem[rem-1]:
        return mem[rem-1]
    else:
        a = min([dp(rem-AB[i][0]) + AB[i][1] for i in range(N)])
        mem[rem-1] = a
        return a
print(dp(H))

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
H,N = (int(x) for x in input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
mem = [0] * H
def dp(rem):
    global mem
    if rem <= 0:
        return 0
    elif mem[rem-1]:
        return mem[rem-1]
    else:
        a = min([dp(rem-AB[i][0]) + AB[i][1] for i in range(N)])
        mem[rem-1] = a
        return a
print(dp(H))

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
H,N = (int(x) for x in input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
mem = [0] * H
def dp(rem):
    global mem
    if mem[rem-1]:
        return mem[rem-1]
    elif rem <= 0:
        return 0
    else:
        a = min([dp(rem-AB[i][0]) + AB[i][1] for i in range(N)])
        mem[rem-1] = a
        return a
print(dp(H))

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
H,N = (int(x) for x in input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
mem = [0] * H
def dp(rem):
    global mem
    if rem <= 0:
        return 0
    elif mem[rem-1]:
        return mem[rem-1]
    else:
        a = min([dp(rem-AB[i][0]) + AB[i][1] for i in range(N)])
        mem[rem-1] = a
        return a
print(dp(H))
Buggy python submission (verdict: Time Limit Exceeded):
import math

def comb(n,r,m):
    if r == 0: return 1
    return math.factorial(n)*pow(f,m-2,m)*pow(math.factorial(n-r),m-2,m)

N,K,*a = map(int, open(0).read().split())
m = 1000000007
f = math.factorial(K-1)
a.sort()
ans = 0
if K == 1:
    print(0)
else:
    for i in range(1,N-K+2):
        ans = (ans + a[-i] * comb(N-i,K-1,m)) % m
    for i in range(1,N-K+2):
        ans = (ans - a[i-1] * comb(N-i,K-1,m)) % m
    print(ans)

Fixed submission:
def memfact(a,m):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % m
        yield temp
def comb(n,r,m):
    if r == 0: return 1
    return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)

N,K,*a = map(int, open(0).read().split())
m = 1000000007
memf = []
mfappend = memf.append
for x in memfact(N-1,m):
    mfappend(x)
a.sort()
ans = 0
if K == 1:
    print(0)
else:
    for i in range(1,N-K+2):
        ans = (ans + a[-i] * comb(N-i,K-1,m)) % m
    for i in range(1,N-K+2):
        ans = (ans - a[i-1] * comb(N-i,K-1,m)) % m
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math

def comb(n,r,m):
    if r == 0: return 1
    return math.factorial(n)*pow(f,m-2,m)*pow(math.factorial(n-r),m-2,m)

N,K,*a = map(int, open(0).read().split())
m = 1000000007
f = math.factorial(K-1)
a.sort()
ans = 0
if K == 1:
    print(0)
else:
    for i in range(1,N-K+2):
        ans = (ans + a[-i] * comb(N-i,K-1,m)) % m
    for i in range(1,N-K+2):
        ans = (ans - a[i-1] * comb(N-i,K-1,m)) % m
    print(ans)

Fixed submission:
def memfact(a,m):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % m
        yield temp
def comb(n,r,m):
    if r == 0: return 1
    return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)

N,K,*a = map(int, open(0).read().split())
m = 1000000007
memf = []
mfappend = memf.append
for x in memfact(N-1,m):
    mfappend(x)
a.sort()
ans = 0
if K == 1:
    print(0)
else:
    for i in range(1,N-K+2):
        ans = (ans + a[-i] * comb(N-i,K-1,m)) % m
    for i in range(1,N-K+2):
        ans = (ans - a[i-1] * comb(N-i,K-1,m)) % m
    print(ans)