Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using llong = long long int;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const int MOD = 1000000007;
const int INF = 1000000000;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

const int MAX = 100001;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  COMinit();
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
  llong n, k;
  cin >> n >> k;
  vector<llong> A(n);
  rep(i, n) cin >> A[i];
  sort(all(A));
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }

  vector<llong> sumf(n + 1, 0), sume(n + 1, 0);
  rep(i, n) {
    sumf[i + 1] += sumf[i] + (A[i] + MOD);
    sumf[i + 1] %= MOD;
  }
  reverse(all(A));
  rep(i, n) {
    sume[i + 1] += sume[i] + (A[i] + MOD);
    sume[i + 1] %= MOD;
  }

  int l = n - 2, r = k - 2, i = 0;
  llong ans = 0;
  while (l >= r) {
    ++i;
    ans += COM(l, r) * (sume[i] - sumf[i] + MOD);
    ans %= MOD;
    --l;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using llong = long long int;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const int MOD = 1000000007;
const int INF = 1000000000;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

const int MAX = 100001;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  COMinit();
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  llong n, k;
  cin >> n >> k;
  vector<llong> A(n);
  rep(i, n) cin >> A[i];
  sort(all(A));
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }

  vector<llong> sumf(n + 1, 0), sume(n + 1, 0);
  rep(i, n) {
    sumf[i + 1] += sumf[i] + (A[i] + MOD);
    sumf[i + 1] %= MOD;
  }
  reverse(all(A));
  rep(i, n) {
    sume[i + 1] += sume[i] + (A[i] + MOD);
    sume[i + 1] %= MOD;
  }

  int l = n - 2, r = k - 2, i = 0;
  llong ans = 0;
  while (l >= r) {
    ++i;
    ans += COM(l, r) * (sume[i] - sumf[i] + MOD);
    ans %= MOD;
    --l;
  }

  cout << ans << endl;

  return 0;
}