Problem p02804 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

const ll N_MAX = 100005;

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll combination(ll C, ll D) {
  if (C < D || C < 1)
    return 0;
  return fac[C] * (finv[D] * finv[C - D] % mod) % mod;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  make();

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  sort(A.begin(), A.end());
  vector<ll> B(N, 0);
  REP(i, 10) B[i] = combination(i, K - 1);

  ll res = 0;
  REP(i, N) {
    ll tmp = B[i] * A[i];
    if (tmp < 0) {
      ll kai = (abs(tmp) / mod);
      tmp += (kai + 2) * mod;
    }
    tmp %= mod;
    res += tmp;
    res %= mod;
  }

  REP(i, N) {
    ll tmp = B[N - i - 1] * A[i];
    if (tmp < 0) {
      ll kai = (abs(tmp) / mod);
      tmp += (kai + 2) * mod;
    }
    tmp %= mod;
    tmp = (tmp + mod) % mod;
    res -= tmp;
    res = (res + mod) % mod;
  }
  cout << res << endl;
  // cout << B << endl;

  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

const ll N_MAX = 100005;

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll combination(ll C, ll D) {
  if (C < D || C < 1)
    return 0;
  return fac[C] * (finv[D] * finv[C - D] % mod) % mod;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  make();

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  sort(A.begin(), A.end());
  vector<ll> B(N, 0);
  REP(i, N) B[i] = combination(i, K - 1);

  ll res = 0;
  REP(i, N) {
    ll tmp = B[i] * A[i];
    if (tmp < 0) {
      ll kai = (abs(tmp) / mod);
      tmp += (kai + 2) * mod;
    }
    tmp %= mod;
    res += tmp;
    res %= mod;
  }

  REP(i, N) {
    ll tmp = B[N - i - 1] * A[i];
    if (tmp < 0) {
      ll kai = (abs(tmp) / mod);
      tmp += (kai + 2) * mod;
    }
    tmp %= mod;
    tmp = (tmp + mod) % mod;
    res -= tmp;
    res = (res + mod) % mod;
  }
  cout << res << endl;
  // cout << B << endl;

  return 0;
}