Problem p02804 (C++)

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

#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define y1 asdgh
#define int long long
#define double long double
#define endl '\n'
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

/*------------------------------<Debugging
 * Stuff>---------------------------------*/

template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R, class M>
ostream &operator<<(ostream &os, tuple<L, R, M> P) {
  return os << "(" << get<0>(P) << "," << get<1>(P) << "," << get<2>(P) << ")";
}
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "{ ";
  for (auto v : V)
    os << v << " ";
  return os << "}";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

/*------------------------------</Debugging
 * Stuff>--------------------------------*/
const int maxn = (int)1e6 + 6;
const double EPS = 1e-9;
const int INF = (int)1e9 + 10;
const int mod = (int)1e9 + 7;

inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1LL * y) % mod; }
inline int binpow(int a, int b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return binpow(a, mod - 2); }

signed main() {
  fio;
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &it : a)
    cin >> it;
  sort(a.begin(), a.end());
  int answer = 0;
  vector<int> fac(n + 1), ifac(n + 1);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = mul(i, fac[i - 1]);
  for (int i = 0; i <= n; i++)
    ifac[i] = inv(fac[i]);
  auto get = [&](int n, int r) {
    return mul(fac[n], mul(ifac[r], ifac[n - r]));
  };
  for (int i = 0; i < n; i++) {
    if (i >= k - 1) {
      answer = add(answer, mul(a[i], get(i, k - 1)));
    }
    if (n - i - 1 >= k - 1) {
      answer = sub(answer, mul(a[i], get(n - i - 1, k - 1)));
    }
  }

  assert(answer >= 0 and answer < mod);
  cout << answer << endl;

  return 0;
}


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

#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define y1 asdgh
#define int long long
#define double long double
#define endl '\n'
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

/*------------------------------<Debugging
 * Stuff>---------------------------------*/

template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R, class M>
ostream &operator<<(ostream &os, tuple<L, R, M> P) {
  return os << "(" << get<0>(P) << "," << get<1>(P) << "," << get<2>(P) << ")";
}
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "{ ";
  for (auto v : V)
    os << v << " ";
  return os << "}";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

/*------------------------------</Debugging
 * Stuff>--------------------------------*/
const int maxn = (int)1e6 + 6;
const double EPS = 1e-9;
const int INF = (int)1e9 + 10;
const int mod = (int)1e9 + 7;

inline int add(int x, int y) { return (x + y) % mod; }
inline int sub(int x, int y) { return (x - y + mod) % mod; }
inline int mul(int x, int y) { return (x * y) % mod; }
inline int binpow(int a, int b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return binpow(a, mod - 2); }

signed main() {
  fio;
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &it : a)
    cin >> it;
  sort(a.begin(), a.end());
  int answer = 0;
  vector<int> fac(n + 1), ifac(n + 1);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = mul(i, fac[i - 1]);
  for (int i = 0; i <= n; i++)
    ifac[i] = inv(fac[i]);
  auto get = [&](int n, int r) {
    return mul(fac[n], mul(ifac[r], ifac[n - r]));
  };
  for (int i = 0; i < n; i++) {
    if (i >= k - 1) {
      answer = add(answer, mul(a[i], get(i, k - 1)));
    }
    if (n - i - 1 >= k - 1) {
      answer = sub(answer, mul(a[i], get(n - i - 1, k - 1)));
    }
  }

  assert(answer >= 0 and answer < mod);
  cout << answer << endl;

  return 0;
}