Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

// int/long:		-2,147,483,648 - 2,147,483,648 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 29)
// 536,870,912

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

/*
int	gcd(int A, int B) {
        while (0!=B) {
                A %=B;
                swap(A, B);
        }
        return A;
}
*/

const int MAX = 100000;
#define MOD 1000000007

void ComInit(vector<long> &fac, vector<long> &finv, vector<long> &inv) {
  fac.at(0) = 1;
  fac.at(1) = 1;
  finv.at(0) = 1;
  finv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - (inv.at(MOD % i) * (MOD / i)) % MOD;
    finv.at(i) = (finv.at(i - 1) * inv.at(i)) % MOD;
  }
}

long COM(int N, int K, vector<long> &fac, vector<long> &finv) {
  if (N < K)
    return 0;
  if ((N < 0) || (K < 0))
    return 0;
  return (fac.at(N) * ((finv.at(K) * finv.at(N - K)) % MOD)) % MOD;
}

int main() {
  // Array
  vector<long> fac(MAX);
  vector<long> finv(MAX);
  vector<long> inv(MAX);
  // Initialize
  ComInit(fac, finv, inv);
  // Start
  long N, K;
  cin >> N >> K;
  vector<long> A(N, 0);
  for (long i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  long S = 0;
  for (long w = K; w <= N; w++) {
    long CC = 1;
    if ((K > 2) && (w - K > 0))
      CC = COM(w - 2, K - 2, fac, finv);
    for (long st = 0; st <= N - w; st++) {
      long f = A.at(st + w - 1) - A.at(st);
      S += f * CC % MOD;
      S %= MOD;
    }
  }
  cout << S << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// int/long:		-2,147,483,648 - 2,147,483,648 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 29)
// 536,870,912

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

/*
int	gcd(int A, int B) {
        while (0!=B) {
                A %=B;
                swap(A, B);
        }
        return A;
}
*/

const int MAX = 100000;
#define MOD 1000000007

void ComInit(vector<long> &fac, vector<long> &finv, vector<long> &inv) {
  fac.at(0) = 1;
  fac.at(1) = 1;
  finv.at(0) = 1;
  finv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - (inv.at(MOD % i) * (MOD / i)) % MOD;
    finv.at(i) = (finv.at(i - 1) * inv.at(i)) % MOD;
  }
}

long COM(int N, int K, vector<long> &fac, vector<long> &finv) {
  if (N < K)
    return 0;
  if ((N < 0) || (K < 0))
    return 0;
  return (fac.at(N) * ((finv.at(K) * finv.at(N - K)) % MOD)) % MOD;
}

int main() {
  // Array
  vector<long> fac(MAX);
  vector<long> finv(MAX);
  vector<long> inv(MAX);
  // Initialize
  ComInit(fac, finv, inv);
  // Start
  long N, K;
  cin >> N >> K;
  vector<long> A(N, 0);
  for (long i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  long S = 0;
  for (long i = K - 1; i < N; i++) {
    S += ((A.at(i) - A.at(N - 1 - i)) * COM(i, K - 1, fac, finv)) % MOD;
    S %= MOD;
  }
  cout << S << endl;
  return 0;
}