Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import numpy as np

h, w, k = map(int, input().split())
grid = np.array([list(map(str, input().rstrip())) for _ in range(h)])

ans = 0
for i in np.array(list(product([0, 1], repeat=h)), np.bool):
    for j in np.array(list(product([0, 1], repeat=w)), np.bool):
        if np.any(i) and np.any(j):
            arr = grid[i].copy()
            arr = arr[:, j]
            arr = arr.flatten()
            if np.where(arr == "#")[0].shape[0] == k:
                ans += 1

print(ans)

Fixed submission:
import numpy as np
from itertools import product

h, w, k = map(int, input().split())
grid = np.array([list(map(str, input().rstrip())) for _ in range(h)])

ans = 0
for i in np.array(list(product([0, 1], repeat=h)), np.bool):
    for j in np.array(list(product([0, 1], repeat=w)), np.bool):
        if np.any(i) and np.any(j):
            arr = grid[i].copy()
            arr = arr[:, j]
            arr = arr.flatten()
            if np.where(arr == "#")[0].shape[0] == k:
                ans += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
d = int(input())
c = list(map(int, input().split()))
s = np.array([list(map(int, input().split())) for _ in range(d)])

init = s.argmax(axis=1) + 1

for i in range(d):
    print(init[i])

Fixed submission:
import numpy as np
d = int(input())
c = list(map(int, input().split()))
s = np.array([list(map(int, input().split())) for _ in range(d)])

init = s.argmax(axis=1) + 1

print(*init, sep="\n")
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from numba import njit


@njit('i8(i4, i4[:])', cache=True)
def solve(n, div):
    for i in range(2, n + 1):
        div[i::i] += 1
    return (div * np.arange(n + 1)).sum()


n = int(input())
div = np.ones(n + 1, dtype=np.int32)
print(solve(n, div))

Fixed submission:
n = int(input())
div = [1] * (n + 1)

for i in range(2, n + 1):
    cnt = 1
    while i * cnt <= n:
        div[i * cnt] += 1
        cnt += 1
print(sum([x * y for x, y in enumerate(div)]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from numba import njit


@njit('i8(i8, i8[:])', cache=True)
def solve(n, div):
    for i in range(2, n + 1):
        div[i::i] += 1
    return (div * np.arange(n + 1)).sum()


n = int(input())
div = np.ones(n + 1, dtype=np.int64)
print(solve(n, div))

Fixed submission:
n = int(input())
div = [1] * (n + 1)

for i in range(2, n + 1):
    cnt = 1
    while i * cnt <= n:
        div[i * cnt] += 1
        cnt += 1
print(sum([x * y for x, y in enumerate(div)]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())

div = np.ones(n + 1, dtype=np.int32)

for i in range(2, n + 1):
    div[i::i] += 1

print((div * np.arange(n + 1)).sum())

Fixed submission:
n = int(input())
div = [1] * (n + 1)

for i in range(2, n + 1):
    cnt = 1
    while i * cnt <= n:
        div[i * cnt] += 1
        cnt += 1
print(sum([x * y for x, y in enumerate(div)]))
Buggy python submission (verdict: Time Limit Exceeded):
a, b, c, d = map(int, input().split())
MOD = 998244353

dp = [[0] * (d - b + 1) for _ in range(c - a + 1)]

dp[0][0] = 1

for x in range(1, d - b + 1):
    dp[0][x] = dp[0][x - 1] * a % MOD
for y in range(1, c - a + 1):
    dp[y][0] = dp[y - 1][0] * b % MOD

for x in range(1, c - a + 1):
    for y in range(1, d - b + 1):
        dp[x][y] = (dp[x - 1][y] * (b + y) + dp[x][y - 1] * (a + x) - dp[x - 1][y - 1] * (a + x - 1) * (b + y - 1)) % MOD

print(dp[-1][-1])

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

dp = [[0] * (d - b + 1) for _ in range(c - a + 1)]

dp[0][0] = 1

for x in range(1, d - b + 1):
    dp[0][x] = dp[0][x - 1] * a % MOD
for y in range(1, c - a + 1):
    dp[y][0] = dp[y - 1][0] * b % MOD

for x in range(1, c - a + 1):
    for y in range(1, d - b + 1):
        dp[x][y] = (dp[x - 1][y] * (b + y) + dp[x][y - 1] * (a + x) - dp[x - 1][y - 1] * (a + x - 1) * (b + y - 1)) % MOD

print(dp[-1][-1])
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

arr.sort()
c = Counter(arr)

cnt = 0

while cnt < len(arr):
    arr = [arr[cnt]] + [x for x in arr if x % arr[cnt] != 0]
    arr.sort()
    cnt += 1

result = set(arr) - {x for x, y in c.items() if y >= 2}

print(len(result))

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

dp = [0] * (10**7)

cnt = 0
for i in range(n):
    if dp[arr[i]] == 0:
        for j in range(1, (10**6 + 10) // arr[i] + 1):
            dp[arr[i] * j] += 1
        if i < n - 1:
            if arr[i] == arr[i + 1]:
                continue
        cnt += 1
    else:
        continue

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

arr.sort()
c = Counter(arr)

cnt = 0

while cnt < len(arr):
    arr = [arr[cnt]] + [x for x in arr if x % arr[cnt] != 0]
    arr.sort()
    cnt += 1

result = set(arr) - {x for x, y in c.items() if y >= 2}

print(len(result))

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

dp = [0] * (10**7)

cnt = 0
for i in range(n):
    if dp[arr[i]] == 0:
        for j in range(1, (10**6 + 10) // arr[i] + 1):
            dp[arr[i] * j] += 1
        if i < n - 1:
            if arr[i] == arr[i + 1]:
                continue
        cnt += 1
    else:
        continue

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from collections import Counter

n = int(input())
arr = np.array(list(map(int, input().split())))

arr.sort()
c = Counter(arr)

cnt = 0

while cnt < arr.shape[0]:
    arr = np.concatenate([np.array([arr[cnt]]), arr[np.where(arr % arr[cnt] != 0)[0]]])
    arr.sort()
    cnt += 1

result = set(arr) - {x for x, y in c.items() if y >= 2}

print(len(result))

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

dp = [0] * (10**7)

cnt = 0
for i in range(n):
    if dp[arr[i]] == 0:
        for j in range(1, (10**6 + 10) // arr[i] + 1):
            dp[arr[i] * j] += 1
        if i < n - 1:
            if arr[i] == arr[i + 1]:
                continue
        cnt += 1
    else:
        continue

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n = int(input())
arr = np.array(list(map(int, input().split())))

arr.sort()

result = np.zeros(n, dtype=np.int32)

for i in range(n):
    if result[i] >= 1:
        continue
    else:
        tmp = np.where(arr % arr[i] == 0)
        result[tmp] += 1
        result[i] -= 1
        if i < n - 1 and arr[i] == arr[i + 1]:
            result[i] += 1

print(np.where(result == 0)[0].shape[0])

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

dp = [0] * (10**7)

cnt = 0
for i in range(n):
    if dp[arr[i]] == 0:
        for j in range(1, (10**6 + 10) // arr[i] + 1):
            dp[arr[i] * j] += 1
        if i < n - 1:
            if arr[i] == arr[i + 1]:
                continue
        cnt += 1
    else:
        continue

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
from operator import mul
n = int(input())
arr = list(map(int, input().split()))

ans = reduce(mul, arr)
print(-1 if ans > 10**18 else ans)

Fixed submission:
import sys
n = int(input())
arr = list(map(int, input().split()))

if 0 in arr:
    print(0)
    sys.exit()

ans = 1
for i in arr:
    ans *= i
    if ans > 10**18:
        print(-1)
        sys.exit()

else:
    print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()[::-1]
length = len(s)
a = [0] * length
d = [0] * length
ans = [0] * 2019
x = 10

a[0] = int(s[0])
d[0] = a[0]
ans[d[0]] += 1

for i in range(1, length):
    a[i] = int(s[i]) * x % 2019
    d[i] = (d[i-1] + a[i]) % 2019
    ans[d[i]] += 1
    x = x * 10 % 2019

print(int(sum([ans[i] * (ans[i]-1) for i in range(2019)]) / 2) + ans[0])

Fixed submission:
s, w = map(int, input().split())
print("unsafe" if s <= w else "safe")

Buggy python submission (verdict: Runtime Error):
s, w = map(int, input())
print("unsafe" if s <= w else "safe")

Fixed submission:
s, w = map(int, input().split())
print("unsafe" if s <= w else "safe")
Buggy python submission (verdict: Runtime Error):
import numpy
a = np.array([i for i in range(1, int(input())+1)])
print(a[np.where((a % 3 != 0) & (a % 5 !=0))].sum())

Fixed submission:
import numpy as np
a = np.array([i for i in range(1, int(input())+1)])
print(a[np.where((a % 3 != 0) & (a % 5 !=0))].sum())
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

n = int(input())
s = input()

ans = 0
for x, y, z in combinations(range(n), 3):
    if y - x != z - y:
        if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
            ans += 1

print(ans)

Fixed submission:
import sys
from operator import mul
from functools import reduce
from collections import Counter

n = int(input())
s = input()

c = Counter(s)
if len(c) != 3:
    print(0)
    sys.exit()

ans = reduce(mul, c.values())

for i in range(n - 1):
    x, y, z = i, i + 1, i + 2
    while z <= n - 1:
        if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
            ans -= 1
        y += 1
        z += 2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

n = int(input())
s = input()

ans = 0
for x, y, z in combinations(range(n), 3):
    if y - x != z - y:
        if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
            ans += 1

print(ans)

Fixed submission:
import sys
from operator import mul
from functools import reduce
from collections import Counter

n = int(input())
s = input()

c = Counter(s)
if len(c) != 3:
    print(0)
    sys.exit()

ans = reduce(mul, c.values())

for i in range(n - 1):
    x, y, z = i, i + 1, i + 2
    while z <= n - 1:
        if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
            ans -= 1
        y += 1
        z += 2

print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
print(max(abs(a1 - a2)))

Fixed submission:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
a1 = np.zeros(1, dtype=int)
a1 = np.append(a1, a)
a2 = a
a2 = np.append(a2, a[0]+k)
print(k - max(a2[1:] - a1[1:]))

Buggy python submission (verdict: Runtime Error):
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
print(max(abs(a1 - a2)))

Fixed submission:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
a1 = np.zeros(1, dtype=int)
a1 = np.append(a1, a)
a2 = a
a2 = np.append(a2, a[0]+k)
print(k - max(a2[1:] - a1[1:]))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)
memo = defaultdict(int)


def combi(n):
    return n * (n-1) // 2


for a in A:
    d[a] += 1

for i in range(N):
    ans = []
    tmp = d.copy()
    tmp[A[i]] -= 1
    tmp = list(tmp.values())
    for t in tmp:
        if memo[t] == 0:
            memo[t] = combi(t)
        ans.append(memo[t])
    print(sum(ans))

Fixed submission:
from collections import defaultdict


def combi(n):
    if n < 2:
        return 0
    return n * (n-1) // 2


N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)

for a in A:
    d[a] += 1

whole = sum(list(map(combi, list(d.values()))))

diff = []
for i in range(N):
    diff.append(combi(d[A[i]]) - combi(d[A[i]] - 1))

for x in diff:
    print(whole - x)

Buggy python submission (verdict: Time Limit Exceeded):
from functools import lru_cache
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)


@lru_cache(maxsize=None)
def combi(n):
    return n * (n-1) // 2


for a in A:
    d[a] += 1

ans = []
for i in range(N):
    tmp = d.copy()
    tmp[A[i]] -= 1
    ans = sum(list(map(combi, list(tmp.values()))))
    print(ans)

Fixed submission:
from collections import defaultdict


def combi(n):
    if n < 2:
        return 0
    return n * (n-1) // 2


N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)

for a in A:
    d[a] += 1

whole = sum(list(map(combi, list(d.values()))))

diff = []
for i in range(N):
    diff.append(combi(d[A[i]]) - combi(d[A[i]] - 1))

for x in diff:
    print(whole - x)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)


def combi(n):
    return n * (n-1) // 2


for a in A:
    d[a] += 1

ans = []
for i in range(N):
    tmp = d.copy()
    tmp[A[i]] -= 1
    ans = sum(list(map(combi, list(tmp.values()))))
    print(ans)

Fixed submission:
from collections import defaultdict


def combi(n):
    if n < 2:
        return 0
    return n * (n-1) // 2


N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)

for a in A:
    d[a] += 1

whole = sum(list(map(combi, list(d.values()))))

diff = []
for i in range(N):
    diff.append(combi(d[A[i]]) - combi(d[A[i]] - 1))

for x in diff:
    print(whole - x)
Buggy python submission (verdict: Runtime Error):
from decimal import Decimal, getcontext
getcontext().prec = 1000

li = list(map(Decimal, input().split()))
li = map(lambda x: x.sqrt(), li)

print("Yes" if a + b < c else "No")

Fixed submission:
from decimal import Decimal
a, b, c = map(Decimal, input().split())

ans = (c - a - b)**2 > 4 * a * b

if c - a - b < 0:
    ans = False

print("Yes" if ans else "No")
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
import numpy as np

A, B, M = map(int, input().split())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))

C = defaultdict(int)

for i in range(M):
    x, y, c = map(int, input().split())
    C[tuple([x-1, y-1])] = c

ans = a.min() + b.min()

for c in C:
    ans = min(ans, a[c[0]] + b[c[0]] - C[c])
    
print(ans)

Fixed submission:
from collections import defaultdict
import numpy as np

A, B, M = map(int, input().split())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))

ans = a.min() + b.min()

for i in range(M):
    x, y, c = map(int, input().split())
    ans = min(ans, a[x-1] + b[y-1] - c)

print(ans)
Buggy python submission (verdict: Runtime Error):
import math
a, b = map(int, input().split())
a = math.ceil(a / 0.08)
b = math.ceil(b / 0.1)
tmp_a = set([i for i in range(a, a+13)])
tmp_b = set([i for i in range(b, b+10)])
if len(tmp_a & tmp_b) == 0:
    print(-1)
else:
    print(min(tmp_a & tmp_b))

Fixed submission:
n, a, b = map(int, input().split())
q, m = divmod(n, a+b)
print(a * q + min(m, a))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

s = deque(list(map(str, input().rstrip())))
q = int(input())

for _ in range(q):
    li = list(map(str, input().split()))
    if li[0] == "1":
        s.reverse()
    elif li[1] == "1":
        s.appendleft(li[2])
    else:
        s.append(li[2])

print("".join(s))

Fixed submission:
from collections import deque

s = deque(list(map(str, input().rstrip())))
q = int(input())

state = 1
for _ in range(q):
    li = list(map(str, input().split()))
    if li[0] == "1":
        state *= -1
    elif (li[1] == "1" and state == 1) or (li[1] == "2" and state == -1):
        s.appendleft(li[2])
    else:
        s.append(li[2])

if state == -1:
    s.reverse()

print("".join(s))
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict


class UnionFind:
    def __init__(self, n):
        self.root = list(range(n + 1))
        self.size = [1] * (n + 1)

    def find(self, x):
        root = self.root
        while root[x] != x:
            root[x] = root[root[x]]
            x = root[x]
        return x

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        sx, sy = self.size[x], self.size[y]
        if sx < sy:
            self.root[x] = y
            self.size[y] += sx
        else:
            self.root[y] = x
            self.size[x] += sy


n, m, k = map(int, input().split())


uf = UnionFind(n)

friends = defaultdict(int)
chain = defaultdict(int)

for _ in range(m):
    a, b = map(int, input().split())
    friends[a] += 1
    friends[b] += 1
    uf.union(a, b)

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if uf.find(i) == uf.find(j):
            chain[i] += 1

for _ in range(k):
    c, d = map(int, input().split())
    if uf.find(c) == uf.find(d):
        friends[c] += 1
        friends[d] += 1

ans = []
for i in range(1, n + 1):
    ans.append(chain[i] - friends[i] - 1)

print(*ans)

Fixed submission:
import math
print(math.ceil(int(input())/2))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
s = [0] * m
c = [0] * m
tmp = [0] * m

for i in range(m):
    s[i], c[i] = map(int, input().split())
    
for x, y in zip(s, c):
    if tmp[x-1]==0:
        tmp[x-1] = y
    elif tmp[x-1]==y:
        continue
    else:
        print(-1)
        exit()

ans = 0

for i, a in enumerate(tmp[::-1]):
    ans += a*10**(i)

if n==1 & ans==0:
    print(ans)
elif ans // 10**(m-1) < 1:
    print(-1)
    exit()
    
print(ans)

Fixed submission:
import math
print(math.ceil(int(input())/2))

Buggy python submission (verdict: Runtime Error):
import sys
n, m = map(int, input().split())
S = [0] * m
C = [0] * m
tmp = [0] * n

for i in range(m):
    S[i], C[i] = map(int, input().split())

for s, c in zip(S, C):
    if tmp[s-1] == 0:
        tmp[s-1] = c
    elif tmp[s-1] == c:
        pass
    else:
        print(-1)
        sys.exit()

ans = 0
for i, a in enumerate(tmp[::-1]):
    ans += a*10**(i)

print(ans)

Fixed submission:
import math
print(math.ceil(int(input())/2))
Buggy python submission (verdict: Runtime Error):
N, R = map(int, input().split())
print(R + 100*(10-N)) if n < 10 else print(R)

Fixed submission:
N, R = map(int, input().split())
print(R + 100*(10-N)) if N < 10 else print(R)
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
MOD = 10**9 + 7


def calc_2(n):
    ans = 1
    for i in range(n):
        ans *= 2
        ans %= MOD
    return ans - 1


def cmb(N, A):
    num = reduce(lambda x, y: x * y % MOD, range(N, N - A, -1))
    den = reduce(lambda x, y: x * y % MOD, range(1, A + 1))
    return num * pow(den, MOD - 2, MOD) % MOD


n, a, b = map(int, input().split())
a %= MOD
b %= MOD


print((calc_2(n) - cmb(n, a) - cmb(n, b)) % MOD)

Fixed submission:
from functools import reduce
MOD = 10**9 + 7


def cmb(N, A):
    num = reduce(lambda x, y: x * y % MOD, range(N, N - A, -1))
    den = reduce(lambda x, y: x * y % MOD, range(1, A + 1))
    return num * pow(den, MOD - 2, MOD) % MOD


n, a, b = map(int, input().split())
a %= MOD
b %= MOD


print((pow(2, n, MOD) - 1 - cmb(n, a) - cmb(n, b)) % MOD)
Buggy python submission (verdict: Runtime Error):
from itertools import accumulate

N, K = map(int, input().split())
p = list(map(int, input().split()))

p = list(accumulate(p))

ans = 0
for i in range(N - K):
    ans = max(ans, p[i + K]-p[i])

ans = (ans + K) / 2

print(ans)

Fixed submission:
s, t = input().split()
a, b = map(int, input().split())
u = input()

if s==u:
    a = a-1
if t==u:
    b = b-1

print(a, b)
Buggy python submission (verdict: Runtime Error):
import math

N = int(input())
P = list(map(int, input().split()))
m = math.inf
ans = 0

for p in P:
    if p <= m:
        ans += 1
        m = p

print(ans)

Fixed submission:
N = int(input())
P = list(map(int, input().split()))
m = float('inf')
ans = 0

for p in P:
    if p <= m:
        ans += 1
        m = p

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
p = np.array(list(map(int, input().split())))

ans = 1

for i in range(1, p.shape[0]):
    tmp = p[:i].copy()
    tmp = tmp - p[i]
    if tmp[:i].min() >= 0:
        ans += 1

print(ans)

Fixed submission:
N = int(input())
P = list(map(int, input().split()))
m = float('inf')
ans = 0

for p in P:
    if p <= m:
        ans += 1
        m = p

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
p = np.array(list(map(int, input().split())))

ans = 1

for i in range(1, p.shape[0]):
    tmp = p.copy()
    tmp = tmp - p[i]
    if tmp[:i+1].min() >= 0:
        ans += 1

print(ans)

Fixed submission:
N = int(input())
P = list(map(int, input().split()))
m = float('inf')
ans = 0

for p in P:
    if p <= m:
        ans += 1
        m = p

print(ans)
Buggy python submission (verdict: Runtime Error):
def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


MOD = 10 ** 9 + 7
facts, invs = prepare(n, MOD)


n, k = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()

ans_max = 0
for i in range(k - 1, n):
    ans_max += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

arr.sort(reverse=True)

ans_min = 0
for i in range(k - 1, n):
    ans_min += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

print((ans_max - ans_min) % MOD)

Fixed submission:
def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


n, k = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()


MOD = 10 ** 9 + 7
facts, invs = prepare(n, MOD)

ans_max = 0
for i in range(k - 1, n):
    ans_max += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

arr.sort(reverse=True)

ans_min = 0
for i in range(k - 1, n):
    ans_min += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

print((ans_max - ans_min) % MOD)

Buggy python submission (verdict: Time Limit Exceeded):
def factorial_mod(n, mod):
    a = 1
    for i in range(1, n + 1):
        a *= i
        a %= mod
    return a


def comb_mod(n, k, mod):
    if k > n or k < 0:
        return 0
    fact_n = factorial_mod(n, mod)
    fact_k = factorial_mod(k, mod)
    fact_n_k = factorial_mod(n - k, mod)
    return (fact_n * pow(fact_k, mod - 2, mod) * pow(fact_n_k, mod - 2, mod)) % mod


n, k = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()

MOD = 10**9 + 7

ans_max = 0
for i in range(k - 1, n):
    ans_max += arr[i] * comb_mod(i, k - 1, MOD)
    ans_max %= MOD

arr.sort(reverse=True)

ans_min = 0
for i in range(k - 1, n):
    ans_min += arr[i] * comb_mod(i, k - 1, MOD)
    ans_min %= MOD

print((ans_max - ans_min) % MOD)

Fixed submission:
def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


n, k = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()


MOD = 10 ** 9 + 7
facts, invs = prepare(n, MOD)

ans_max = 0
for i in range(k - 1, n):
    ans_max += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

arr.sort(reverse=True)

ans_min = 0
for i in range(k - 1, n):
    ans_min += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

print((ans_max - ans_min) % MOD)
Buggy python submission (verdict: Runtime Error):
import math

a, b = map(int, input().split())
print(int(a * b / math.gcd(a, b)))

Fixed submission:
def gcd(a, b):
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

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

print(a*b // gcd(a, b))