Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#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...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

int64 power_mod(int64 a, int n) {
  int64 ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
  }
  return ret;
}
const int N = 1e5 + 10;
int F[N], G[N];

int64 comb(int n, int m) { return (int64)F[n] * G[m] % MOD * G[n - m] % MOD; }

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  F[0] = G[0] = 1;
  for (int i = 1; i <= n; ++i) {
    F[i] = (int64)F[i - 1] * i % MOD;
    G[i] = power_mod(F[i], MOD - 2);
  }
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  sort(a.begin(), a.end());
  int ret = 0;
  for (int i = 1; i < n; ++i) {
    int L = i, R = n - i;
    int64 cur = comb(n, m);
    cur = (cur + MOD - comb(R, m)) % MOD;
    cur = (cur + MOD - comb(L, m)) % MOD;
    // trace(i, cur, a[i] - a[i - 1]);
    ret += cur * (a[i] - a[i - 1]) % MOD;
    ret %= MOD;
  }
  printf("%d\n", ret);
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#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...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

int64 power_mod(int64 a, int n) {
  int64 ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
  }
  return ret;
}
const int N = 1e5 + 10;
int F[N], G[N];

int64 comb(int n, int m) {
  if (m < 0 || m > n)
    return 0;
  return (int64)F[n] * G[m] % MOD * G[n - m] % MOD;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  F[0] = G[0] = 1;
  for (int i = 1; i <= n; ++i) {
    F[i] = (int64)F[i - 1] * i % MOD;
    G[i] = power_mod(F[i], MOD - 2);
  }
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  sort(a.begin(), a.end());
  int ret = 0;
  for (int i = 1; i < n; ++i) {
    int L = i, R = n - i;
    int64 cur = comb(n, m);
    cur = (cur + MOD - comb(R, m)) % MOD;
    cur = (cur + MOD - comb(L, m)) % MOD;
    // trace(i, cur, a[i] - a[i - 1]);
    ret += cur * (a[i] - a[i - 1]) % MOD;
    ret %= MOD;
  }
  printf("%d\n", ret);
  return 0;
}