Buggy python submission (verdict: Runtime Error):
m = 10 ** 9 + 7
f = [1]

for i in range(10 ** 5 + 5):
    f.append(f[-1] * (i + 1) % m)

def nCr(n,r,mod = m):
    return f[n] * pow(f[r],m - 2,m) * pow(f[n-r],m - 2,m) % m

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

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

a.sort()

ans = 0
for i in range(n - k + 1):
    ans += (a[-1-i] - a[i]) * nCr(n - 1 - i,k - 1)
    ans %= m

print(ans)


Fixed submission:
from collections import deque

def bfs(loc):
    global tem_ans
    d = [[float('inf')] * w for i in range(h)]
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    que = deque([])
    que.append(loc)
    d[loc[0]][loc[1]] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] == '.' and d[nx][ny] == float('inf'):
                d[nx][ny] = d[p[0]][p[1]] + 1
                que.append([nx,ny])
                if tem_ans < d[nx][ny]:
                    tem_ans = d[nx][ny]
    return tem_ans

h,w = map(int,input().split())

maze = [list(input()) for i in range(h)]


ans = 0
tem_ans = 0

loc = []
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            loc = [i,j]
        else:
            continue
        ans = max(ans,bfs(loc))

print(ans)

Buggy python submission (verdict: Runtime Error):
from collection import deque

def bfs(loc):
    global tem_ans
    d = [[float('inf')] * w for i in range(h)]

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

    que = deque([])
    que.append(loc)
    d[loc[0]][loc[1]] = 0

    while que:
        p = popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] == '.' and d[nx][ny] == float('inf'):
                d[nx][ny] = d[p[0]][p[1]] + 1
                que.append([nx,ny])

                if tem_ans < d[nx][ny]:
                    tem_ans = d[nx][ny]
    return tem_ans

h,w = map(int,input().split())
maze = [list(input()) for i in range(h)]
ans = 0
tem_ans = 0

loc = []
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            loc = [i,j]
        else:
            continue
        ans = max(ans,bfs(loc))

print(ans)


Fixed submission:
from collections import deque

def bfs(loc):
    global tem_ans
    d = [[float('inf')] * w for i in range(h)]
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    que = deque([])
    que.append(loc)
    d[loc[0]][loc[1]] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] == '.' and d[nx][ny] == float('inf'):
                d[nx][ny] = d[p[0]][p[1]] + 1
                que.append([nx,ny])
                if tem_ans < d[nx][ny]:
                    tem_ans = d[nx][ny]
    return tem_ans

h,w = map(int,input().split())

maze = [list(input()) for i in range(h)]


ans = 0
tem_ans = 0

loc = []
for i in range(h):
    for j in range(w):
        if maze[i][j] != '#':
            loc = [i,j]
        else:
            continue
        ans = max(ans,bfs(loc))

print(ans)