Problem p02804 (C++)

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

template <typename T> istream &operator>>(istream &istr, vector<T> &vec) {
  for (T &x : vec)
    istr >> x;
  return istr;
}

template <typename T> ostream &operator<<(ostream &ostr, vector<T> &vec) {
  if (!vec.empty()) {
    ostr << vec.front();
    for (auto itr = ++vec.begin(); itr != vec.end(); itr++)
      ostr << " " << *itr;
  }
  return ostr;
}

template <typename T>
ostream &operator<<(ostream &ostr, vector<vector<T>> &mat) {
  if (!mat.empty()) {
    ostr << mat.front();
    for (auto itr = ++mat.begin(); itr != mat.end(); itr++)
      ostr << endl << *itr;
  }
  return ostr;
}

template <typename T, typename U>
ostream &operator<<(ostream &ostr, pair<T, U> &p) {
  ostr << p.first << " " << p.second;
  return ostr;
}

template <typename T, typename U>
ostream &operator<<(ostream &ostr, vector<pair<T, U>> &mat) {
  if (!mat.empty()) {
    ostr << mat.front();
    for (auto itr = ++mat.begin(); itr != mat.end(); itr++)
      ostr << endl << *itr;
  }
  return ostr;
}

typedef long long ll;
typedef vector<ll> vll;
typedef vector<char> vc;
// vll x(n); x[i]=a;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
// vvll x(m,vi(n)); x[i][j]=a;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
// vpll x(n); x[i]={a,b}; x[i].first/second;

#define PI 3.1415926535897932
#define INF 1e+12
#define EPS 1e-12
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define dump(x) cout << #x << " = " << (x) << endl
#define all(x) (x).begin(), (x).end()
// sort(all(x));

// 拡張ユーグリッド互除法
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll g = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return g;
}

ll gcd(ll a, ll b) {
  ll x, y;
  return extgcd(a, b, x, y);
}

// 逆元(mod p)
ll modinv(ll a, ll p) {
  ll x, y;
  extgcd(a, p, x, y);
  if (x < 0)
    x += p;
  return x;
}

// 二項係数
#define MAX 10000
ll fac[MAX + 1];
ll finv[MAX + 1];
void nCr_init(ll max, ll mod) {
  fac[0] = 1;
  finv[0] = 1;
  REP(i, max) { // i=1->max+1 => 0->max
    fac[i + 1] = fac[i] * (i + 1) % mod;
    finv[i + 1] = finv[i] * modinv(i + 1, mod) % mod;
  }
}

ll nCr(ll n, ll r, ll mod) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % mod) % mod;
}

int main() {
  const ll MOD = 1e9 + 7;
  ll n, k, ans = 0;
  cin >> n >> k;
  vll a(n);
  cin >> a;
  sort(all(a));

  nCr_init(n, MOD);

  REP(i, n) {
    ans += a[i] * nCr(i, k - 1, MOD);
    ans -= a[i] * nCr(n - i - 1, k - 1, MOD);
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}


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

template <typename T> istream &operator>>(istream &istr, vector<T> &vec) {
  for (T &x : vec)
    istr >> x;
  return istr;
}

template <typename T> ostream &operator<<(ostream &ostr, vector<T> &vec) {
  if (!vec.empty()) {
    ostr << vec.front();
    for (auto itr = ++vec.begin(); itr != vec.end(); itr++)
      ostr << " " << *itr;
  }
  return ostr;
}

template <typename T>
ostream &operator<<(ostream &ostr, vector<vector<T>> &mat) {
  if (!mat.empty()) {
    ostr << mat.front();
    for (auto itr = ++mat.begin(); itr != mat.end(); itr++)
      ostr << endl << *itr;
  }
  return ostr;
}

template <typename T, typename U>
ostream &operator<<(ostream &ostr, pair<T, U> &p) {
  ostr << p.first << " " << p.second;
  return ostr;
}

template <typename T, typename U>
ostream &operator<<(ostream &ostr, vector<pair<T, U>> &mat) {
  if (!mat.empty()) {
    ostr << mat.front();
    for (auto itr = ++mat.begin(); itr != mat.end(); itr++)
      ostr << endl << *itr;
  }
  return ostr;
}

typedef long long ll;
typedef vector<ll> vll;
typedef vector<char> vc;
// vll x(n); x[i]=a;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
// vvll x(m,vi(n)); x[i][j]=a;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
// vpll x(n); x[i]={a,b}; x[i].first/second;

#define PI 3.1415926535897932
#define INF 1e+12
#define EPS 1e-12
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define dump(x) cout << #x << " = " << (x) << endl
#define all(x) (x).begin(), (x).end()
// sort(all(x));

// 拡張ユーグリッド互除法
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll g = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return g;
}

ll gcd(ll a, ll b) {
  ll x, y;
  return extgcd(a, b, x, y);
}

// 逆元(mod p)
ll modinv(ll a, ll p) {
  ll x, y;
  extgcd(a, p, x, y);
  if (x < 0)
    x += p;
  return x;
}

// 二項係数
#define MAX (int)1e5
ll fac[MAX + 1];
ll finv[MAX + 1];
void nCr_init(ll max, ll mod) {
  fac[0] = 1;
  finv[0] = 1;
  REP(i, max) { // i=1->max+1 => 0->max
    fac[i + 1] = fac[i] * (i + 1) % mod;
    finv[i + 1] = finv[i] * modinv(i + 1, mod) % mod;
  }
}

ll nCr(ll n, ll r, ll mod) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % mod) % mod;
}

int main() {
  const ll MOD = 1e9 + 7;
  ll n, k, ans = 0;
  cin >> n >> k;
  vll a(n);
  cin >> a;
  sort(all(a));

  nCr_init(n, MOD);

  REP(i, n) {
    ans += a[i] * nCr(i, k - 1, MOD);
    ans -= a[i] * nCr(n - i - 1, k - 1, MOD);
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}