Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<int, null_type, std::less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
//  Там еще некоторые особенности есть. Подробнее
//  https://codeforces.com/blog/entry/11080?locale=ru

using namespace std;
using ld = long double;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define len(x) (int)(x).size()
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

const ll mod = (ll)1e9 + 7;

ll binpowmod(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= a;
      res %= mod;
    }
    n >>= 1ll;
    a *= a;
    a %= mod;
  }
  return res;
}

inline ll inv(ll a) { return binpowmod(a, mod - 2); }

void solve() {
  int n, k;
  cin >> n >> k;

  vll a(n);
  for (auto &i : a) {
    cin >> i;
  }

  sort(all(a));

  vll facts(n + 1, 1);
  for (ll i = 2; i <= n; i++) {
    facts[i] = facts[i - 1] * i;
    facts[i] %= mod;
  }

  vll invfacts(n + 1, 1);
  for (ll i = 2; i <= n; i++) {
    invfacts[i] = inv(facts[i]);
  }

  vll C(n + 1, 1);
  for (int i = k - 2; i <= n; i++) {
    C[i] = ((facts[i] * invfacts[k - 2]) % mod * invfacts[i - (k - 2)]) % mod;
  }

  ll ans = 0;

  int step = 0;
  ll sum = 0;
  for (int i = k - 1; i < n; i++) {
    sum += C[k - 2 + step++];
    sum %= mod;
    ll val = (a[i] + mod) % mod;
    ans = (ans + (val * sum) % mod) % mod;
  }

  step = 0;
  sum = 0;
  for (int i = n - k; i >= 0; --i) {
    sum -= C[k - 2 + step++];
    sum = (sum + mod) % mod;
    ll val = (a[i] + mod) % mod;
    ans = (ans + (val * sum) % mod) % mod;
  }

  cout << ans;
}

int main() {
  int t = 1;
  for (int i = 0; i < t; i++) {
    solve();
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<int, null_type, std::less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
//  Там еще некоторые особенности есть. Подробнее
//  https://codeforces.com/blog/entry/11080?locale=ru

using namespace std;
using ld = long double;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define len(x) (int)(x).size()
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

const ll mod = (ll)1e9 + 7;

ll binpowmod(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= a;
      res %= mod;
    }
    n >>= 1ll;
    a *= a;
    a %= mod;
  }
  return res;
}

inline ll inv(ll a) { return binpowmod(a, mod - 2); }

void solve() {
  int n, k;
  cin >> n >> k;

  vll a(n);
  for (auto &i : a) {
    cin >> i;
  }

  if (k == 1) {
    cout << 0;
    return;
  }

  sort(all(a));

  vll facts(n + 1, 1);
  for (ll i = 2; i <= n; i++) {
    facts[i] = facts[i - 1] * i;
    facts[i] %= mod;
  }

  vll invfacts(n + 1, 1);
  for (ll i = 2; i <= n; i++) {
    invfacts[i] = inv(facts[i]);
  }

  vll C(n + 1, 1);
  for (int i = k - 2; i <= n; i++) {
    C[i] = ((facts[i] * invfacts[k - 2]) % mod * invfacts[i - (k - 2)]) % mod;
  }

  ll ans = 0;

  int step = 0;
  ll sum = 0;
  for (int i = k - 1; i < n; i++) {
    sum += C[k - 2 + step++];
    sum %= mod;
    ll val = (a[i] + mod) % mod;
    ans = (ans + (val * sum) % mod) % mod;
  }

  step = 0;
  sum = 0;
  for (int i = n - k; i >= 0; --i) {
    sum -= C[k - 2 + step++];
    sum = (sum + mod) % mod;
    ll val = (a[i] + mod) % mod;
    ans = (ans + (val * sum) % mod) % mod;
  }

  cout << ans;
}

int main() {
  int t = 1;
  for (int i = 0; i < t; i++) {
    solve();
    cout << endl;
  }
  return 0;
}