Problem p02804 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 51000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
lli nCr(lli n, lli r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % mod) % mod;
}
int main() {
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N >> K;
  Vec(P, lli, N, 0);
  COMinit();
  rep(i, 0, N) cin >> P[i];
  Sort(P);
  A = 0;
  B = 0;
  rep(i, K - 1, N) {
    A += P[i] * nCr(i, K - 1);
    A += mod;
    A %= mod;
  }
  Rev(P);
  rep(i, K - 1, N) {
    B += P[i] * nCr(i, K - 1);
    B += mod;
    B %= mod;
  }
  Out((A - B + mod) % mod);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 510000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
lli nCr(lli n, lli r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % mod) % mod;
}
int main() {
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N >> K;
  Vec(P, lli, N, 0);
  COMinit();
  rep(i, 0, N) cin >> P[i];
  Sort(P);
  A = 0;
  B = 0;
  rep(i, K - 1, N) {
    A += P[i] * nCr(i, K - 1);
    A += mod;
    A %= mod;
  }
  Rev(P);
  rep(i, K - 1, N) {
    B += P[i] * nCr(i, K - 1);
    B += mod;
    B %= mod;
  }
  Out((A - B + mod) % mod);
}