qyliang/CodeNanoFix huggingface dataset:
Buggy code:

Copy
Copy
from collections import deque
h,w = map(int,input().split(" "))
s = [input() for _ in range(h)]
 
ans = 0
direcion = [(1,0),(0,1),(-1,0),(0,-1)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        que = deque([(j,i)])
        dist = [[0]*w for _ in range(h)]
        visited = set()
        visited.add((i,j))
        while que:
            j, i = que.popleft()
            for x,y in direcion:
                n_x,n_y = j+x,i+y
                if not 0 <= n_x < w: continue
                if not 0 <= n_y < h: continue
                if (n_y,n_x) in visited: continue
                if s[n_y][n_x] == "#": continue
                visited.add((n_y,n_x))
                dist[n_y][n_x] = dist[i][j] + 1
                que.append((n_x,n_y))
            ans = max(ans,max(max(row) for row in dist))
print(ans)

Fixed code:
from collections import deque
h,w = map(int,input().split(" "))
s = [input() for _ in range(h)]
 
ans = 0
direcion = [(1,0),(0,1),(-1,0),(0,-1)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        que = deque([(j,i)])
        dist = [[0]*w for _ in range(h)]
        visited = [[0]*w for _ in range(h)]
        visited[i][j] = 1
        while que:
            j, i = que.popleft()
            for x,y in direcion:
                n_x,n_y = j+x,i+y
                if not 0 <= n_x < w: continue
                if not 0 <= n_y < h: continue
                if visited[n_y][n_x]: continue
                if s[n_y][n_x] == "#": continue
                visited[n_y][n_x] = 1
                dist[n_y][n_x] = dist[i][j] + 1
                que.append((n_x,n_y))
        ans = max(ans,max(max(row) for row in dist))
print(ans)
Buggy code:
from collections import deque
import numpy as np
H,W = map(int,input().split())
Maze=[list(map(int,input().split())) for i in range(H)]
for hi in range(0,H):
    for wi in range(0,W):
        if Maze[hi][wi]=="#":
            continue
        maze1=[[0]*W for _ in range(H)]
        stack=deque([[hi,wi]])
        while stack:
            h,w=stack.popleft()
            for i,j in [[1,0],[-1,0],[0,1],[0,-1]]:
                new_h,new_w=h+i,w+j
                if new_h <0 or new_w <0 or new_h >=H or new_w >=W:
                    continue
                elif Maze[new_h][new_w]!="#" and maze1[new_h][new_w]==0:
                    maze1[new_h][new_w]+=maze1[h][w]
                    stack.append([new_h,new_w])
        
        maze1[hi][wi]=0
        ans=max(ans,np.max(maze1))
print(ans)

Fixed code:
from collections import deque
import numpy as np
H,W = map(int,input().split())
Maze=[list(input()) for i in range(H)]
ans=0
for hi in range(0,H):
    for wi in range(0,W):
        if Maze[hi][wi]=="#":
            continue
        maze1=[[0]*W for _ in range(H)]
        stack=deque([[hi,wi]])
        while stack:
            h,w=stack.popleft()
            for i,j in [[1,0],[-1,0],[0,1],[0,-1]]:
                new_h,new_w=h+i,w+j
                if new_h <0 or new_w <0 or new_h >=H or new_w >=W:
                    continue
                elif Maze[new_h][new_w]!="#" and maze1[new_h][new_w]==0:
                    maze1[new_h][new_w]=maze1[h][w]+1
                    stack.append([new_h,new_w])
        
        maze1[hi][wi]=0
        ans=max(ans,np.max(maze1))
print(ans)
Buggy code:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline

H, W = map(int, readline().split())
S = [list(readline()) for i in range(H)]


def bfs(grid, start):
    distance = [[-1] * W for _ in range(H)]
    queue = deque([start])
    while queue:
        x, y = queue.popleft()

        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == '.' and distance[new_x][new_y] == -1:
                    distance[new_x][new_y] = distance[x][y] + 1
                    queue.append((new_x, new_y))
    return max(map(max, distance))


answer = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue

        tmp = bfs(S, (i, j))
        if tmp > answer:
            answer = tmp

print(answer)


Fixed code:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline

H, W = map(int, readline().split())
S = [list(input()) for i in range(H)]


def bfs(grid, start):
    distance = [[0] * W for _ in range(H)]
    queue = deque([start])
    visited = [[False] * W for _ in range(H)]
    visited[start[0]][start[1]] = True
    while queue:
        x, y = queue.popleft()

        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == '.' and not visited[new_x][new_y]:
                    distance[new_x][new_y] = distance[x][y] + 1
                    visited[new_x][new_y] = True
                    queue.append((new_x, new_y))
    return distance


answer = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue

        tmp = max(map(max, bfs(S, (i, j))))
        if tmp > answer:
            answer = tmp

print(answer)
Buggy code:
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
grid = [sr() for _ in range(H)]

def bfs(sy, sx):
    d = [[float('inf')] * W for _ in range(H)]
    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    que = deque()
    que.append((sy, sx))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            ny = p[0] + dy[i]
            nx = p[1] + dx[i]                
            if 0 <= ny < H and 0 <= nx < W and \
                    grid[ny][nx] != '#' and d[ny][nx] == float('inf'):
                d[ny][nx] = d[p[0]][p[1]] + 1
                que.append((ny, nx))
    max = 0
    for row in d:
        for r in row:
            if r != float('inf') and r > max:
                max = r
    return max

answer = 0    
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        result = bfs(i, j)
        if result > answer:
            answer = result

print(answer)
# 49


Fixed code:
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
grid = [sr() for _ in range(H)]

def bfs(sy, sx):
    d = [[float('inf')] * W for _ in range(H)]
    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    que = deque()
    que.append((sy, sx))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            ny = p[0] + dy[i]
            nx = p[1] + dx[i]                
            if 0 <= ny < H and 0 <= nx < W and \
                    grid[ny][nx] != '#' and d[ny][nx] == float('inf'):
                d[ny][nx] = d[p[0]][p[1]] + 1
                que.append((ny, nx))
    max = 0
    for row in d:
        for r in row:
            if r != float('inf') and r > max:
                max = r
    return max

answer = 0    
for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            continue
        result = bfs(i, j)
        if result > answer:
            answer = result

print(answer)
# 49
Buggy code:
import queue

dx = [0, 0, -1, -1]
dy = [1, -1, 0, 0]

h, w = map(int, input().split())
s = []
for i in range(h) :
    s.append(input())

ans = 0

for i in range(h) :
    for j in range(w) :
        if s[i][j] == '#' :
            continue
        dist = [[-1 for i in range(h)] for j in range(w)]
        q = queue.Queue()
        q.put([j,i])
        dist[j][i] = 0
        cnt = 0
        while not q.empty() :
            xy = q.get()
            for k in range(4) :
                x = xy[0] + dx[k]
                y = xy[1] + dy[k]
                if y < 0 or y >= h or x < 0 or x >= w :
                    continue
                if s[y][x] == '#' :
                    continue
                if dist[x][y] != -1 :
                    continue
                q.put([x, y])
                dist[x][y] = dist[xy[0]][xy[1]] + 1

for i in range(h) :
    for j in range(w) :
        ans = max(ans, dist[j][i])


print(ans)


Fixed code:
import queue

dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]

h, w = map(int, input().split())
s = []
for i in range(h) :
    s.append(input())

ans = 0

for i in range(h) :
    for j in range(w) :
        if s[i][j] == '#' :
            continue
        dist = [[-1 for i in range(w)] for j in range(h)]
        q = queue.Queue()
        q.put([j,i])
        dist[i][j] = 0
        cnt = 0
        while not q.empty() :
            xy = q.get()
            for k in range(4) :
                x = xy[0] + dx[k]
                y = xy[1] + dy[k]
                if y < 0 or y >= h or x < 0 or x >= w :
                    continue
                if s[y][x] == '#' :
                    continue
                if dist[y][x] != -1 :
                    continue
                q.put([x, y])
                dist[y][x] = dist[xy[1]][xy[0]] + 1
        for k in range(h) :
            for l in range(w) :
                ans = max(ans, dist[k][l])


print(ans)
Buggy code:
p = 10**9+7
def pow(x,m):
    if m==0:
        return 1
    if m==1:
        return x
    if m%2==0:
        return (pow(x,m//2)**2)%p
    else:
        return (x*(pow(x,(m-1)//2)**2)%p)%p
N,K = map(int,input().split())
Z = list(map(int,input().split()))
A = [1 for _ in range(N+1)]
for i in range(2,N+1):
    A[i] = (A[i-1]*i)%p
B = [1 for _ in range(N+1)]
B[N] = pow(A[N],p-2)
for i in range(N-1,1,-1):
    B[i] = (B[i+1]*(i+1))%p
def f(n,k):
    if k>n:
        return 0
    return (A[n]*B[k]*B[n-k])%p
X = sorted(Z,reverse=True)
X.insert(0,0)
ans = 0
for i in range(1,N-K+2):
    ans += X[i]*f(N-i,K-1)
Y = sorted(Z)
Y.insert(0,0)
for i in range(1,N-K+2):
    ans -= Y[i]*f(N-i,K-1)
print(ans)

Fixed code:
p = 10**9+7
def pow(x,m):
    if m==0:
        return 1
    if m==1:
        return x
    if m%2==0:
        return (pow(x,m//2)**2)%p
    else:
        return (x*(pow(x,(m-1)//2)**2)%p)%p
N,K = map(int,input().split())
Z = list(map(int,input().split()))
A = [1 for _ in range(N+1)]
for i in range(2,N+1):
    A[i] = (A[i-1]*i)%p
B = [1 for _ in range(N+1)]
B[N] = pow(A[N],p-2)
for i in range(N-1,1,-1):
    B[i] = (B[i+1]*(i+1))%p
def f(n,k):
    if k>n:
        return 0
    return (A[n]*B[k]*B[n-k])%p
X = sorted(Z,reverse=True)
X.insert(0,0)
ans = 0
for i in range(1,N-K+2):
    ans = (ans+X[i]*f(N-i,K-1))%p
Y = sorted(Z)
Y.insert(0,0)
for i in range(1,N-K+2):
    ans = (ans-Y[i]*f(N-i,K-1))%p
print(ans)
Buggy code:
import itertools

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=10**9+7
ans=0
a.sort()
a=list(itertools.accumulate(a))
pin=0


lst=[1,1]
for i in range(2,n+10):
  lst.append((lst[-1]*i)%mod)

def combinations(n,r):
  xxx=lst[n]
  xxx*=pow(lst[n-r],mod-2,mod)
  xxx%=mod
  xxx*=pow(lst[r],mod-2,mod)
  xxx%=mod

  return xxx


for i in range(n-k+1):
  pin+=1
  now=n-k+1-i
  ans+=(a[-1]-a[-now-1]-a[now-1])*combinations(pin+k-3,k-2)
  ans%=mod


print(ans)

Fixed code:
import itertools

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=10**9+7
ans=0
a.sort()
a=list(itertools.accumulate(a))
pin=0


lst=[1,1]
for i in range(2,n+10):
  lst.append((lst[-1]*i)%mod)

def combinations(n,r):
  xxx=lst[n]
  xxx*=pow(lst[n-r],mod-2,mod)
  xxx%=mod
  xxx*=pow(lst[r],mod-2,mod)
  xxx%=mod

  return xxx

if k==1:
    print(0)
    exit()


for i in range(n-k+1):
  pin+=1
  now=n-k+1-i
  ans+=(a[-1]-a[-now-1]-a[now-1])*combinations(pin+k-3,k-2)
  ans%=mod


print(ans)
Buggy code:
# modular inverse for positive a and b and nCk mod MOD depending on modinv

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)

def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

MOD = 10 ** 9 + 7

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

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

Combination = []

for i in range(N):
    if i < K:
        Combination.append(0)
    elif i == K:
        Combination.append(1)
    else:
        Combination.append(Combination[-1] * i * modinv(i - K + 1) % MOD)

ans = 0

for i in range(N):
    ans += a[i] * Combination[i]
    ans -= a[i] * Combination[N - i - 1]
    ans %= MOD

print(ans)

Fixed code:
# modular inverse for positive a and b and nCk mod MOD depending on modinv

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)

def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

MOD = 10 ** 9 + 7

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

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

Combination = []

for i in range(N):
    if i < K - 1:
        Combination.append(0)
    elif i == K - 1:
        Combination.append(1)
    else:
        Combination.append(Combination[-1] * i * modinv(i - K + 1, MOD) % MOD)

        
ans = 0

for i in range(N):
    ans += a[i] * Combination[i]
    ans -= a[i] * Combination[N - i - 1]
    ans %= MOD

print(ans)
Buggy code:
MAX = 600000
MOD = 10 ** 9 + 7
fac = [0] * MAX
ifac = [0] * MAX
fac[0] = 1
for i in range(1,MAX):
    fac[i] = (fac[i-1] * i) % MOD
ifac[MAX-1] = pow(fac[MAX-1],MOD-2,MOD)
for i in reversed(range(1,MAX)):
    ifac[i-1] = (ifac[i] * i) % MOD

def combinations(n, k):
    if k < 0 or n < k:
        return 0
    else:
        return (fac[n] * ifac[k] * ifac[n-k]) % MOD

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

def f(A):
    ans = 0
    for i in range(K-1,N):
        ans += A[i]*combinations(i-1,K-1)
        ans %= MOD
    return ans

print((f(A) - f(reversed(A))) % MOD )



Fixed code:
MAX = 600000
MOD = 10 ** 9 + 7
fac = [0] * MAX
ifac = [0] * MAX
fac[0] = 1
for i in range(1,MAX):
    fac[i] = (fac[i-1] * i) % MOD
ifac[MAX-1] = pow(fac[MAX-1],MOD-2,MOD)
for i in reversed(range(1,MAX)):
    ifac[i-1] = (ifac[i] * i) % MOD

def combinations(n, k):
    if k < 0 or n < k:
        return 0
    else:
        return (fac[n] * ifac[k] * ifac[n-k]) % MOD


N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
com = [0]
for i in range(N):
    com.append(A[i] + com[-1])
#print(com)
ans = 0
for i in range(K-2,N-1):
    ans += combinations(i,K-2) * (-(com[N-i-1]) + (com[N]-com[i+1]))
    #print(N-i-1,N,i+1)
    ans %= MOD
    #print(combinations(i,K-2) * (-(com[N-i-1]) + (com[N]-com[i+1])))
print(ans)
Buggy code:
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 code:
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 code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
p = [1]
mod = 10**9+7
for i in range(n):
    p.append(p[-1]*(i+2)%mod)
def comb(n,k,mod):
    s = p[n-1]*pow(fac(k),mod-2,mod)%mod
    s = s*pow(fac(n-k),mod-2,mod)%mod
    return s
max_sum = 0
min_sum = 0
for i in range(n-k+1):
    max_sum += a[-(i+1)]*comb(n-1-i,k-1,mod)
    max_sum = max_sum%mod
    min_sum += a[i]*comb(n-1-i,k-1,mod)
    min_sum = min_sum%mod
ans = max_sum-min_sum
if ans < 0:
    ans += mod
print(ans)

Fixed code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
p = [1]
mod = 10**9+7
for i in range(n):
    p.append(p[-1]*(i+1)%mod)
p.pop()
def comb(n,k,mod):
    s = p[n]*pow(p[k],mod-2,mod)%mod
    s = s*pow(p[n-k],mod-2,mod)%mod
    return s
max_sum = 0
min_sum = 0
for i in range(n-k+1):
    max_sum += a[-(i+1)]*comb(n-1-i,k-1,mod)
    max_sum = max_sum%mod
    min_sum += a[i]*comb(n-1-i,k-1,mod)
    min_sum = min_sum%mod
ans = max_sum-min_sum
if ans < 0:
    ans += mod
print(ans)
Buggy code:
#

import sys
input=sys.stdin.readline
MOD=10**9+7

fact=[1]*(10**5+1)
for i in range(1,10**5+1):
    fact[i]=(fact[i-1]*i)%MOD
    
def comb(n,r):
    nhri=pow(fact[n-r],MOD-2,MOD)
    ri=pow(fact[r],MOD-2,MOD)
    return fact[n]//(nhri*ri)


def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    
    A.sort()
    maxsum=0
    for i in range(K-1,N):
        maxsum+=comb(i,K-1)*A[i]
        maxsum%=MOD
    
    A.sort(reverse=True)
    minsum=0
    for i in range(K-1,N):
        minsum+=comb(i,K-1)*A[i]
        minsum%=MOD
        
    print((maxsum-minsum)%MOD)
    
    
if __name__=="__main__":
    main()


Fixed code:
#

import sys
input=sys.stdin.readline
MOD=10**9+7

fact=[1]*(10**5+1)
for i in range(1,10**5+1):
    fact[i]=(fact[i-1]*i)%MOD
    
def comb(n,r):
    nhri=pow(fact[n-r],MOD-2,MOD)
    ri=pow(fact[r],MOD-2,MOD)
    return fact[n]*nhri*ri


def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    
    A.sort()
    maxsum=0
    for i in range(K-1,N):
        maxsum+=comb(i,K-1)*A[i]
        maxsum%=MOD
    
    A.sort(reverse=True)
    minsum=0
    for i in range(K-1,N):
        minsum+=comb(i,K-1)*A[i]
        minsum%=MOD
        
    print((maxsum-minsum)%MOD)
    
    
if __name__=="__main__":
    main()
Buggy code:
def COMinit(N, mod):
    fac = [0] * N
    finv = [0] * N
    inv = [0] * N

    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1

    for i in range(2, N):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i] * (mod // i) % mod
        finv[i] = finv[i-1] * inv[i] % mod
    
    return fac, finv, inv

def COM(n, r, mod, fac, finv, inv):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n-r] % mod) % mod

def solve():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9 + 7
    A.sort()

    fac, finv, inv = COMinit(N+1,MOD)
    max_sm = 0
    min_sm = 0
    for i in range(N):
        smallerNums = i
        largerNums = N - i - 1
        max_sm += A[i] * COM(smallerNums, K-1, MOD, fac, finv, inv) % MOD
        min_sm += A[i] * COM(largerNums, K-1, MOD, fac, finv, inv) % MOD
    
    print(max_sm % MOD - min_sm % MOD)

if __name__ == '__main__':
    solve()

Fixed code:
def COMinit(N, mod):
    fac = [0] * N
    finv = [0] * N
    inv = [0] * N

    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1

    for i in range(2, N):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i] * (mod // i) % mod
        finv[i] = finv[i-1] * inv[i] % mod
    
    return fac, finv, inv

def COM(n, r, mod, fac, finv, inv):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n-r] % mod) % mod

def solve():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9 + 7
    A.sort()

    fac, finv, inv = COMinit(N+1,MOD)
    max_sm = 0
    min_sm = 0
    for i in range(N):
        smallerNums = i
        largerNums = N - i - 1
        max_sm += A[i] * COM(smallerNums, K-1, MOD, fac, finv, inv) % MOD
        min_sm += A[i] * COM(largerNums, K-1, MOD, fac, finv, inv) % MOD
    
    print((max_sm % MOD - min_sm % MOD) % MOD)

if __name__ == '__main__':
    solve()
Buggy code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
mod=10**9+7
fan=[1]*N+1
for i in range(2,N+1):
  fan[i]=fan[i-1]*(i)%mod
ans=0
def comb(a,b):
  return fan[a]*pow(fan[b],mod-2,mod)*pow(fan[a-b],mod-2,mod)%mod
for i in range(N-K+1):
  for j in range(0,i-K+1)
  ans+=comb(i-j-1,K-1)*(A[N-1-i]-A[j])
  ans%=mod
print(ans)

Fixed code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
MOD=10**9+7
fan=[1]*(N+1)
for i in range(2,N+1):
    fan[i]=fan[i-1]*(i)%MOD
ans=0
def comb(a,b):
    return fan[a]*pow(fan[b],MOD-2,MOD)*pow(fan[a-b],MOD-2,MOD)%MOD
for i in range(N-K+1):
    ans=ans+comb(N-i-1,K-1)*(A[N-1-i]-A[i])
    ans%=MOD
print(ans)
Buggy code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
mod=10**9+7
if k==1:
    print(sum(A)%mod)
else:
    X=[]
    A.sort()
    x=sum(A[k-1:])
    y=sum(A[:n-k+1])
    for i in range(k,n+1):
        X.append(x-y)
        x-=A[i-1]
        y-=A[n-i]

    ans=0
    cmb=1
    for i in range(k-2,n-1):
        ans+=cmb*X[i-(k-2)]
        ans%=mod
        cmb*=(i+1)*pow(i-(k-3),mod-2,mod)
        cmb%=mod
    print(ans)

Fixed code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
mod=10**9+7
if k==1:
    print(0)
else:
    X=[]
    A.sort()
    x=sum(A[k-1:])
    y=sum(A[:n-k+1])
    for i in range(k,n+1):
        X.append(x-y)
        x-=A[i-1]
        y-=A[n-i]

    ans=0
    cmb=1
    for i in range(k-2,n-1):
        ans+=cmb*X[i-(k-2)]
        ans%=mod
        cmb*=(i+1)*pow(i-(k-3),mod-2,mod)
        cmb%=mod
    print(ans)
Buggy code:
#comb_mod(n, r, mod) = nCr % mod
def comb_mod(n, r, mod):
    ans = 1
    if r <= n/2:
        for i in range(n-r+1, n+1):
            ans *= i
            ans %= mod
        for i in range(1, r+1):
            ans *= pow(i, mod-2, mod)
            ans %= mod
    else:
        for i in range(r+1, n+1):
            ans *= i
            ans %= mod
        for i in range(1, n-r+1):
            ans *= pow(i, mod-2, mod)
            ans %= mod
    return ans

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

print(a)

ans = 0
for i in range(n-k+1):
    ans += (a[-(i+1)] - a[i]) * comb_mod(n-i-1, k-1, 10**9+7)
print(ans % (10**9+7))

Fixed code:
#comb_mod(n, r, mod) = nCr % mod
def comb_mod(n, r, mod):
    ans = 1
    if r <= n/2:
        for i in range(n-r+1, n+1):
            ans *= i
            ans %= mod
        for i in range(1, r+1):
            ans *= pow(i, mod-2, mod)
            ans %= mod
    else:
        for i in range(r+1, n+1):
            ans *= i
            ans %= mod
        for i in range(1, n-r+1):
            ans *= pow(i, mod-2, mod)
            ans %= mod
    return ans

#comb(n,r) = nCr
def comb(n, r):
    m = 1
    for i in range(n-r+1, n+1):
        m *= i
    for i in range(1, r+1):
        m //= i
    return m

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

ans = 0
pattern = comb_mod(n-1, k-1, 10**9+7)
for i in range(n-k+1):
    ans += (a[-(i+1)] - a[i]) * pattern
    pattern *= (n-k-i) * pow(n-1-i, 10**9+5, 10**9+7)
    pattern %= 10**9 + 7
print(ans % (10**9+7))