Problem p02804 (python)

Fixed submission:
# 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 submission (verdict: Runtime Error):
#include<algorithm>
using namespace std;

long long combination[100001];
long long numbers[100001];
#define MOD 1000000007

long long gcdExtended(long long,long long,long long*,long long*);

// Function to find modulo inverse of a 
long long modInverse(int a, int m) 
{ 
    long long x, y; 
    long long g = gcdExtended(a, m, &x, &y); \
    // m is added to handle negative x 
    long long res = (x%m + m) % m; 
    return res;
} 
  
// C function for extended Euclidean Algorithm 
long long gcdExtended(long long a, long long b, long long *x, long long *y) 
{ 
    // Base Case 
    if (a == 0) 
    { 
        *x = 0, *y = 1; 
        return b; 
    } 
  
    long long x1, y1; // To store results of recursive call 
    long long gcd = gcdExtended(b%a, a, &x1, &y1); 
  
    // Update x and y using results of recursive 
    // call 
    *x = y1 - (b/a) * x1; 
    *y = x1; 
  
    return gcd; 
} 

int main()
{
    int N,K;
    scanf("%d %d",&N,&K);
    for(int i = 0; i < N; i++)
    {
        scanf("%lld",numbers + i);
    }
    sort(numbers,numbers + N);

    for(int i = 0; i < N; i ++)
    {
        if(i < K - 1)
        {
            combination[i] = 0;
        }
        else if(i == K - 1)
        {
            combination[i] = 1;
        }
        else
        {
            combination[i] =((combination[i-1] * i) % MOD * modInverse(i - K + 1, MOD) % MOD);
        }
    }

    long long ans = 0;
    for(int i = 0; i < N; i ++)
    {
        ans += numbers[i] * combination[i];
        ans -= numbers[i] * combination[N-1-i];
        ans %= MOD;
        ans += MOD;
        ans %= MOD;
    }
    printf("%lld",ans % MOD);
}

Buggy submission (verdict: Runtime Error):
# 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)