Problem p02804 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
 
class Combination():
    # コンストラクタ
    def __init__(self, N:int, P:int):
        self.N = N
        self.P = P
 
        # fact[i] = (i! mod P)
        self.fact = [1, 1]   
        # factinv[i] = ((i!)^(-1) mod P)
        self.factinv = [1, 1]
        # factinv 計算用
        self.inv = [0, 1]    
 
        for i in range(2, N+1):
            self.fact.append((self.fact[-1] * i) % P)
            self.inv.append((-self.inv[P % i] * (P // i)) % P)
            self.factinv.append((self.factinv[-1] * self.inv[-1]) % P)
    
    # nCk (mod P)   (ただし、n<=N)
    def getComb(self, n:int, k:int):
        if (k < 0) or (n < k):
            return 0
        k = min(k, n - k)
        return self.fact[n] * self.factinv[k] * self.factinv[n-k] % self.P
 
def main():
    N,K = map(int,input().split())
    A = sorted([int(i) for i in input().split()])
 
    mod = 10**9+7
 
    COMB = Combination(N,mod)
 
    cmb = [0] * (N-K+1)
    for i in range(N-K+1):
        cmb[i] = COMB.getComb(i+(K-1),K-1)
 
    sum_max = 0
    for i in range(K-1,N):
        sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod
 
    sum_min = 0
    for i in range(N-K,-1,-1):
        sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod
 
    answer = sum_max - sum_min
    print(answer % mod)
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a_sort = sorted(list(map(int, input().split())))

MOD = 10**9 + 7

def SUM(a):
    count_max = 0
    count_min = 0
    comb = 1
    for i in range(len(a)):
        if i == k-1:
            count_max += a[i]
            count_min += a[-i-1]
        elif i > k-1:
            comb = comb * i // (i-k+1)
            count_max = (count_max + (comb * a[i])) % MOD
            count_min = (count_min + (comb * a[-i-1])) % MOD
    return (count_max - count_min + MOD) % MOD

Max = SUM(a_sort)
Min = SUM(a_sort.reverse())

print((Max - Min + MOD) % MOD)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a_sort = sorted(list(map(int, input().split())))

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a):
        if i == k-1:
            count += num
        elif i > k-1:
            comb = comb * i // (i-k+1)
            count = (count + (comb * num)) % (10**9+7)
    return count

Max = SUM(a_sort)
Min = SUM(a_sort.reverse())

print((Max - Min + (10**9+7)) % (10**9+7))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a_sort = sorted(list(map(int, input().split())))

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a):
        if i == k-1:
            count += num
        elif i > k-1:
            comb = comb * i // (i-k+1)
            count = (count + (comb * num)) % (10**9+7)
    return count

Max = SUM(a_sort)
Min = SUM(a_sort.reverse()

print((Max - Min + (10**9+7)) % (10**9+7))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

n, k = map(int, input().split())
# a = list(map(int, input().split()))
a_sort = [int(i) for i in input().split()]

Min = 0
Max = 0

comb_min = 1
comb_max = 1

# a_sort = sorted(a)
def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a_sort):
        print('here', i)
        if i == k-1:
            count += num
        elif i > k-1:
            comb = comb * i / (i-k+1)
            print(comb)
            count += (comb * num) % (10**9+7)
            count = count % (10**9+7)
    return int(count)

Min = SUM(a_sort)
Max = SUM(a_sort.reverse())
print(Min)
print(Max)

print((Min - Max + (10**9+7)) % (10**9+7))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

Min = 0
Max = 0

comb_min = 1
comb_max = 1

a_sort = sorted(a)

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a_sort):
        if i == k-1:
            count += num
        elif i >= k-1:
            comb = comb * i / (i-k+1)
            count += (comb * num) % (10**9+7)
            count = count % (10**9+7)
    return int(count)

Min = SUM(a_sort)
Max = SUM(a_sort.reverse())

print((Min - Max + (10**9+7)) % (10**9+7))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

Min = 0
Max = 0

comb_min = 1
comb_max = 1

a_sort = sorted(a)

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a_sort):
        if i == k-1:
            count += num
        elif i >= k-1:
            comb = comb * i / (i-k+1)
            count += (comb * num) % (10e08+7)
            count = count % (10e08+7)
    return count

Min = SUM(a_sort)
Max = SUM(a_sort.reverse())

print(int( (Min - Max + (10e08+7)) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

Min = 0
Max = 0

comb_min = 1
comb_max = 1

a_sort = sorted(a)

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a_sort):
        if i == k-1:
            count += num
        elif i >= k-1:
            comb = comb * i / (i-k+1)
            count += divmod((comb * num) , (10e08+7))[1]
            count = count % (10e08+7)
    return count

Min = SUM(a_sort)
Max = SUM(a_sort.reverse())

print(int( (Min - Max + (10e08+7)) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

Min = 0
Max = 0

comb_min = 1
comb_max = 1

a_sort = sorted(a)

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a_sort):
        if i == k-1:
            count += num
        elif i >= k-1:
            comb = comb * i / (i-k+1)
            count += (comb * num) % (10e08+7)
            count = count % (10e08+7)
    return count

Min = SUM(a_sort)
Max = SUM(a_sort.reverse())

print(int( (Min - Max + (10e08+7)) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

Min = 0
Max = 0

comb_min = 1
comb_max = 1

a_sort = sorted(a)

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a_sort):
        if i == k-1:
            count += num
        elif i >= k-1:
            comb = comb * i / (i-k+1)
            count += comb * num
            count = count % (10e08+7)
    return count

Min = SUM(a_sort)
Max = SUM(a_sort.reverse())

print(int( (Min - Max + (10e08+7)) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

Min = 0
Max = 0

comb_min = 1
comb_max = 1

a_sort = sorted(a)

def SUM(a):
    count = 0
    comb = 1
    for i, num in enumerate(a_sort):
        if i == k-1:
            count += num
        elif i >= k-1:
            comb = comb * i / (i-k+1)
            count += comb * num
    return count

Min = SUM(a_sort)
Max = SUM(a_sort.reverse())

print(int((Min - Max) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
from scipy.misc import comb

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

Min = 0
Max = 0

a_sort = sorted(a)
for i, num in enumerate(a_sort):
    if i >= k-1:
        Min += comb(i, k-1) * num
    if n-i >= k:
        Max += comb(n-i-1, k-1) * num

print(int((Min - Max) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
from scipy.misc import comb

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

Min = 0
Max = 0

a_sort = sorted(a)
for i, num in enumerate(a_sort):
    if i >= k-1:
        Min += comb(i, k-1, exact=True) * num
    if n-i >= k:
        Max += comb(n-i-1, k-1, exact=True) * num

print(int((Min - Max) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
from scipy.special import comb

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

Min = 0
Max = 0

a_sort = sorted(a)
for i, num in enumerate(a_sort):
    if i >= k-1:
        Min += comb(i, k-1, exact=True) * num
    if n-i >= k:
        Max += comb(n-i-1, k-1, exact=True) * num

print(int((Min - Max) % (10e08+7)))

Buggy submission (verdict: Runtime Error):
from scipy.special import comb

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

Min = 0
Max = 0

a_sort = sorted(a)
for i, num in enumerate(a_sort):
    if i >= k-1:
        Min += comb(i, k-1, exact=True) * num
    if n-i >= k:
        Max += comb(n-i-1, k-1, exact=True) * num

print(int((Min - Max) % (10e08+7)))
print(Min - Max)