Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define endl '\n'
#define Bedoazim                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

const long long mod = 1e9 + 7, N = 1e3 + 5;
long long fact[N], inv[N];

long long ncr(long long n, long long r) {

  return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod;
}

int fastpower(int base, int power) {

  if (power == 0)
    return 1;

  long long halfpower = fastpower(base, power / 2) % mod;

  long long ret = (halfpower * halfpower) % mod;

  if (power % 2) {

    ret = (ret * base) % mod;
  }
  return ret;
}

void pre() {

  fact[0] = 1;

  inv[0] = 1;

  for (long long i = 1; i < N; i++) {

    fact[i] = ((i)*fact[i - 1]) % mod;

    inv[i] = fastpower(fact[i], mod - 2) % mod;
  }
}

int main() {

  Bedoazim

  pre();

  long long n, k, max, min, ans = 0;

  vector<long long> v;

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    long long j;

    cin >> j;

    v.push_back(j);
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < n; i++) {

    if (i < k - 1)
      max = 0;

    else
      max = ncr(i, k - 1);

    if (n - i - 1 < k - 1)
      min = 0;

    else
      min = ncr(n - i - 1, k - 1);

    ans += ((v[i] % mod) * ((max - min) % mod) % mod);

    ans %= mod;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define endl '\n'
#define Bedoazim                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

const long long mod = 1e9 + 7, N = 1e6 + 5;
long long fact[N], inv[N];

long long ncr(long long n, long long r) {

  return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod;
}

int fastpower(int base, int power) {

  if (power == 0)
    return 1;

  long long halfpower = fastpower(base, power / 2) % mod;

  long long ret = (halfpower * halfpower) % mod;

  if (power % 2) {

    ret = (ret * base) % mod;
  }
  return ret;
}

void pre() {

  fact[0] = 1;

  inv[0] = 1;

  for (long long i = 1; i < N; i++) {

    fact[i] = ((i)*fact[i - 1]) % mod;

    inv[i] = fastpower(fact[i], mod - 2) % mod;
  }
}

int main() {

  Bedoazim

  pre();

  long long n, k, max, min, ans = 0;

  vector<long long> v;

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    long long j;

    cin >> j;

    v.push_back(j);
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < n; i++) {

    if (i < k - 1)
      max = 0;

    else
      max = ncr(i, k - 1);

    if (n - i - 1 < k - 1)
      min = 0;

    else
      min = ncr(n - i - 1, k - 1);

    ans += ((v[i] % mod) * ((max - min) % mod) % mod);

    ans %= mod;
  }

  cout << ans << endl;
}