Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#pragma comment(linker, "/STACK:256000000")
using namespace std;

#define in(s) freopen(s, "r", stdin)
#define out(s) freopen(s, "w", stdout)
#define forn(i, n) for (int i = 0; i < n; i++)
#define endl '\n'
#define mp make_pair

typedef long long ll;
typedef long double ld;

const int MAXN = (int)1e5 + 5;
const ll mod = (ll)1e+9 + 7ll;

ll fact[MAXN];

inline ll bin_pow(ll a, ll n) {
  ll u = a, res = 1ll;
  while (n) {
    if (n % 2ll == 1ll)
      res = (res * u) % mod;
    u = (u * u) % mod;
    n /= 2ll;
  }
  return res;
}

inline ll div_mod(ll a, ll b) { return a * bin_pow(b, mod - 2ll) % mod; }

inline ll C(int n, int k) {
  return div_mod(fact[n], fact[n - k] * fact[k] % mod);
}

int main() {
  // in("input.txt");
  // out("output.txt");
  srand(1373737);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  fact[0] = 1ll;
  for (int i = 1; i < MAXN; i++)
    fact[i] = (fact[i - 1] * (ll)i) % mod;

  int n, k;
  cin >> n >> k;

  vector<ll> arr(n);
  forn(i, n) cin >> arr[i];
  sort(arr.begin(), arr.end());

  ll ans = 0ll;
  forn(i, n - 1) {
    int left = i + 1, right = n - i - 1;
    ll all = C(left + right, k);
    ll bad = (C(left, k) + C(right, k)) % mod;
    ll good = (all + mod - bad) % mod;
    ans = (ans + (arr[i + 1] - arr[i]) * good % mod) % mod;
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#pragma comment(linker, "/STACK:256000000")
using namespace std;

#define in(s) freopen(s, "r", stdin)
#define out(s) freopen(s, "w", stdout)
#define forn(i, n) for (int i = 0; i < n; i++)
#define endl '\n'
#define mp make_pair

typedef long long ll;
typedef long double ld;

const int MAXN = (int)1e5 + 5;
const ll mod = (ll)1e+9 + 7ll;

ll fact[MAXN];

inline ll bin_pow(ll a, ll n) {
  ll u = a, res = 1ll;
  while (n) {
    if (n % 2ll == 1ll)
      res = (res * u) % mod;
    u = (u * u) % mod;
    n /= 2ll;
  }
  return res;
}

inline ll div_mod(ll a, ll b) { return a * bin_pow(b, mod - 2ll) % mod; }

inline ll C(int n, int k) {
  if (n < k)
    return 0ll;
  return div_mod(fact[n], fact[n - k] * fact[k] % mod);
}

int main() {
  // in("input.txt");
  // out("output.txt");
  srand(1373737);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  fact[0] = 1ll;
  for (int i = 1; i < MAXN; i++)
    fact[i] = (fact[i - 1] * (ll)i) % mod;

  int n, k;
  cin >> n >> k;

  vector<ll> arr(n);
  forn(i, n) cin >> arr[i];
  sort(arr.begin(), arr.end());

  ll ans = 0ll;
  forn(i, n - 1) {
    int left = i + 1, right = n - i - 1;
    ll all = C(left + right, k);
    ll bad = (C(left, k) + C(right, k)) % mod;
    ll good = (all + mod - bad) % mod;
    ans = (ans + (arr[i + 1] - arr[i]) * good % mod) % mod;
  }
  cout << ans;

  return 0;
}