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

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
Ar = A[::-1]
Ai = [Ar[i] - A[i] for i in range(N)]
ans = 0

if K > N//2:
    n, r = N-1, K-1 
    C = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    for i in range(N-K+1):
        ans += Ai[i] * C
        C *= N-i-K
        if N-i-1 != 0:
            C //= N-i-1
else:
    C = 1
    for i in range(N-K, -1, -1):
        ans += Ai[i] * C
        C *= N - i
        if N-K+1 - i != 0:
            C //= N-K+1 - i

print(ans % 1000000007)

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
Ar = A[::-1]
Ai = [Ar[i] - A[i] for i in range(N)]
ans = 0

if K > N//2:
    n, r = N-1, K-1 
    C = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    for i in range(N-K+1):
        ans += Ai[i] * C
        C *= N-i-K
        if N-i-1 != 0:
            C //= N-i-1
else:
    C = 1
    for i in range(N-K, -1, -1):
        ans += Ai[i] * C
        C *= N - i
        if N-K+1 - i != 0:
            C //= N-K+1 - i

print(ans % 1000000007)

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
Ar = A[::-1]
Ai = [Ar[i] - A[i] for i in range(N)]
ans = 0

n, r = N-1, K-1 
C = 1
for i in range(N-K, -1, -1):
    ans += Ai[i] * C
    C *= N - i
    if N-K+1 - i != 0:
        C //= N-K+1 - i

print(ans % 1000000007)

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)

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

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
Ar = A[::-1]
Ai = [Ar[i] - A[i] for i in range(N)]
ans = 0

n, r = N-1, K-1 
C = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

for i in range(N-K+1):
    ans += Ai[i] * C
    C *= N-i-K
    if N-i-1 != 0:
        C //= N-i-1

print(ans % 1000000007)

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)

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

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
ans = 0

n, r = N-1, K-1 
C = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

for i in range(N-K+1):
    ans += (A[-i-1] - A[i]) * C
    C *= N-i-K
    if N-i-1 != 0:
        C //= N-i-1

print(ans % 1000000007)

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)

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

def C(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
ans = 0

C = C(N-1, K-1)
for i in range(N-K+1):
    ans += (A[-i-1] - A[i]) * C
    C *= N-i-K
    if N-i-1 != 0:
        C /= N-i-1

print(int(ans % 1000000007))

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)

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

def C(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
ans = 0

for i in range(N-K+1):
    ans += (A[-i-1] - A[i]) * C(N-i-1, K-1)

print(ans % 1000000007)

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)