prithivMLmods/Coder-Stat huggingface dataset:
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#ifdef ZERO_IQ
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif

long long const mod = 1e9 + 7;

vector<ll> fact(1e5 + 1), inv(1e5 + 1);

ll fp(ll base, ll exp) {
  if (exp == 0)
    return 1;
  ll ans = fp(base, exp / 2);
  ans = (ans * ans) % mod;
  if (exp % 2 != 0)
    ans = (ans * (base % mod)) % mod;
  return ans;
}

void calcFacAndInv(ll n) {
  fact[0] = inv[0] = 1;
  for (ll i = 1; i <= n; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
    inv[i] = fp(fact[i], mod - 2);
  }
}

ll ncr(ll n, ll r) { return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod; }

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  calcFacAndInv(1e5 + 1);
  sort(v.begin(), v.end());
  ll ans = 0;
  for (int j = 0; j < n; ++j) {
    if (j < m - 1 && n - j - 1 >= m - 1) {
      ans = ((ans % mod) +
             (((v[j] % mod) * -ncr(n - j - 1, m - 1)) % mod) % mod) %
            mod;
    } else if (n - j - 1 < m - 1 && j >= m - 1) {
      ans = ((ans % mod) + (((v[j] % mod) * ncr(j, m - 1)) % mod) % mod) % mod;
    } else
      ans = ((ans % mod) +
             ((((v[j] % mod) * (ncr(j, m - 1) - ncr(n - j - 1, m - 1)) % mod) %
               mod) %
              mod)) %
            mod;
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#ifdef ZERO_IQ
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif

long long const mod = 1e9 + 7;

vector<ll> fact(1e5 + 1), inv(1e5 + 1);

ll fp(ll base, ll exp) {
  if (exp == 0)
    return 1;
  ll ans = fp(base, exp / 2);
  ans = (ans * ans) % mod;
  if (exp % 2 != 0)
    ans = (ans * (base % mod)) % mod;
  return ans;
}

void calcFacAndInv(ll n) {
  fact[0] = inv[0] = 1;
  for (ll i = 1; i <= n; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
    inv[i] = fp(fact[i], mod - 2);
  }
}

ll ncr(ll n, ll r) { return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod; }

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  calcFacAndInv(1e5 + 1);
  sort(v.begin(), v.end());
  ll ans = 0;
  for (int j = 0; j < n; ++j) {
    if (j < m - 1 && n - j - 1 >= m - 1) {
      ans = ((ans % mod) +
             (((v[j] % mod) * -ncr(n - j - 1, m - 1)) % mod) % mod) %
            mod;
    } else if (n - j - 1 < m - 1 && j >= m - 1) {
      ans = ((ans % mod) + (((v[j] % mod) * ncr(j, m - 1)) % mod) % mod) % mod;
    } else if (n - j - 1 >= m - 1 && j >= m - 1)
      ans = ((ans % mod) +
             ((((v[j] % mod) * (ncr(j, m - 1) - ncr(n - j - 1, m - 1)) % mod) %
               mod) %
              mod)) %
            mod;
  }
  cout << ans;

  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++)
#define debug(x) cerr << #x << ":" << x << endl
const int maxn = (int)1e6 + 5;
int a[maxn];
typedef long long ll;
ll mod = 1e9 + 7; // 1e9+9

ll add(ll x, ll y) { return (x + y) % mod; }
ll mul(ll x, ll y) { return (x * y) % mod; }

ll mpow(ll x, ll y) {
  ll v = 1;
  for (; y; x = mul(x, x), y >>= 1)
    if (y & 1)
      v = mul(v, x);
  return v;
}

ll divi(ll x, ll y) { return mul(x, mpow(y, mod - 2)); }

ll fact[10000000] = {1}, factc = 0;
ll Fact(ll x) {
  while (factc < x) {
    factc++;
    fact[factc] = mul(fact[factc - 1], factc);
  }
  return fact[x];
}

ll nCr(ll n, ll r) { return divi(Fact(n), mul(Fact(n - r), Fact(r))); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  debug(nCr(1, 2));
  int n, k;
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  sort(a + 1, a + 1 + n);
  ll mx = 0, mn = 0;
  ;
  rep(i, 1, n) {
    mx += a[i] * nCr(i - 1, k - 1);
    mn += a[n - i + 1] * nCr(i - 1, k - 1);
    mx = (mx % mod + mod) % mod;
    mn = (mn % mod + mod) % mod;
    // debug(mx),debug(mn);
  }
  cout << ((mx - mn) % mod + mod) % mod << endl;
}
/*

 */

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++)
#define debug(x) cerr << #x << ":" << x << endl
const int maxn = (int)1e6 + 5;
int a[maxn];
typedef long long ll;
ll mod = 1e9 + 7; // 1e9+9

ll add(ll x, ll y) { return (x + y) % mod; }
ll mul(ll x, ll y) { return (x * y) % mod; }

ll mpow(ll x, ll y) {
  ll v = 1;
  for (; y; x = mul(x, x), y >>= 1)
    if (y & 1)
      v = mul(v, x);
  return v;
}

ll divi(ll x, ll y) { return mul(x, mpow(y, mod - 2)); }

ll fact[10000000] = {1}, factc = 0;
ll Fact(ll x) {
  while (factc < x) {
    factc++;
    fact[factc] = mul(fact[factc - 1], factc);
  }
  return fact[x];
}

ll nCr(ll n, ll r) {
  if (n < r)
    return 0;
  return divi(Fact(n), mul(Fact(n - r), Fact(r)));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  debug(nCr(1, 2));
  int n, k;
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  sort(a + 1, a + 1 + n);
  ll mx = 0, mn = 0;
  ;
  rep(i, 1, n) {
    mx += a[i] * nCr(i - 1, k - 1);
    mn += a[n - i + 1] * nCr(i - 1, k - 1);
    mx = (mx % mod + mod) % mod;
    mn = (mn % mod + mod) % mod;
    // debug(mx),debug(mn);
  }
  cout << ((mx - mn) % mod + mod) % mod << endl;
}
/*

 */
Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;

ll mod(ll &a) { return ((a %= MOD) > 0 ? a : a + MOD); }

const int MAXN = 1e5 + 10;
ll fact_[MAXN] = {0};

ll fact(ll n) {
  if (n == 1)
    return fact_[n] = 1;
  if (fact_[n] != 0)
    return fact_[n];
  return fact_[n] = (n * fact(n - 1)) % MOD;
}

// aのb乗
ll powMod(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    if (res == 0)
      res = MOD;
    a = a * a % MOD;
    if (a == 0)
      a = MOD;
    b >>= 1;
  }
  return res;
}

ll comb(ll a, ll b) {
  if (a == b || b == 0)
    return 1;
  return (((fact(a) * powMod(fact(b), MOD - 2)) % MOD) *
          powMod(fact(a - b), MOD - 2)) %
         MOD;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &a : A)
    cin >> a;
  sort(A.begin(), A.end());
  ll ans = 0;
  for (ll n = 0; n <= N - K; n++) {
    ans += (A[N - 1 - n] - A[n]) * comb(N - 1 - n, K - 1);
    while (ans < 0)
      ans += MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;

ll mod(ll &a) { return ((a %= MOD) > 0 ? a : a + MOD); }

const int MAXN = 1e5 + 10;
ll fact_[MAXN] = {0};

ll fact(ll n) {
  if (n == 1)
    return fact_[n] = 1;
  if (fact_[n] != 0)
    return fact_[n];
  return fact_[n] = (n * fact(n - 1)) % MOD;
}

// aのb乗
ll powMod(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    if (res == 0)
      res = MOD;
    a = a * a % MOD;
    if (a == 0)
      a = MOD;
    b >>= 1;
  }
  return res;
}

ll comb(ll a, ll b) {
  if (a == b || b == 0)
    return 1;
  return (((fact(a) * powMod(fact(b), MOD - 2)) % MOD) *
          powMod(fact(a - b), MOD - 2)) %
         MOD;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &a : A)
    cin >> a;
  sort(A.begin(), A.end());
  ll ans = 0;
  for (ll n = 0; n <= N - K; n++) {
    ll plus = ((A[N - 1 - n] - A[n]) * comb(N - 1 - n, K - 1)) % MOD;
    if (plus < 0)
      plus += MOD;
    ans += plus;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma endregion

#pragma region Combination
const int MAX = 1000000;
const int MOD = 1000000007;

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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));
  COMinit();
  ll res = 0;
  for (int i = k - 1; i < n; ++i) {
    res += a[i] * COM(i, k - 1);
    res %= MOD;
  }
  for (int i = 0; i + k - 1 < n; ++i) {
    res -= a[i] * COM(n - i - 1, k - 1);
    while (res < 0)
      res += MOD;
  }
  cout << res << endl;
}

Fixed submission:
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma endregion

#pragma region Combination
const int MAX = 1000000;
const int MOD = 1000000007;

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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));
  COMinit();
  ll res = 0;
  for (int i = k - 1; i < n; ++i) {
    res += a[i] * COM(i, k - 1);
    res %= MOD;
  }
  for (int i = 0; i + k - 1 < n; ++i) {
    res -= a[i] * COM(n - i - 1, k - 1);
    if (res < 0)
      res -= (res - MOD + 1) / MOD * MOD;
    res %= MOD;
  }
  cout << res << endl;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int A[100005];
const int mod = 1e9 + 7;
int inv(int x) {
  int ans = 1;
  int n = mod - 2;
  while (n) {
    if (n & 1) {
      ans = (ans * x) % mod;
    }
    x = (x * x) % mod;
    n >>= 1;
  }
  return ans;
}
int f[100005];
void init(int n) {
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = (f[i - 1] * i) % mod;
  }
}
int C(int n, int m) { return ((f[n] * inv(f[m])) % mod * inv(f[n - m])) % mod; }
signed main() {

  int n, k;
  scanf("%lld%lld", &n, &k);
  init(n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &A[i]);
  }
  sort(A + 1, A + n + 1);
  int a = 0, b = 0;
  for (int i = 1; i <= n; i++) {
    if (i >= k)
      b = (b + A[i]) % mod;
    if (i <= n - k + 1) {
      a = (a + A[i]) % mod;
    }
  }
  int x = k, y = n - k + 1, ans = 0;

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

    ans = (ans + ((b - a + mod) % mod * C(k + i - 2, k - 2)) % mod) % mod;
    a = (a - A[y--] + mod) % mod;
    b = (b - A[x++] + mod) % mod;
  }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int A[100005];
const int mod = 1e9 + 7;
int inv(int x) {
  int ans = 1;
  int n = mod - 2;
  while (n) {
    if (n & 1) {
      ans = (ans * x) % mod;
    }
    x = (x * x) % mod;
    n >>= 1;
  }
  return ans;
}
int f[100005];
void init(int n) {
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = (f[i - 1] * i) % mod;
  }
}
int C(int n, int m) { return ((f[n] * inv(f[m])) % mod * inv(f[n - m])) % mod; }
signed main() {

  int n, k;
  scanf("%lld%lld", &n, &k);
  init(n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &A[i]);
  }
  sort(A + 1, A + n + 1);
  int a = 0, b = 0;
  for (int i = 1; i <= n; i++) {
    if (i >= k)
      b = (b + A[i]) % mod;
    if (i <= n - k + 1) {
      a = (a + A[i]) % mod;
    }
  }
  int x = k, y = n - k + 1, ans = 0;

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

    ans = (ans + ((b - a + mod) % mod * C(k + i - 2, k - 2)) % mod) % mod;
    if (y > 0)
      a = (a - A[y--] + mod) % mod;
    if (x <= n)
      b = (b - A[x++] + mod) % mod;
  }
  cout << ans << '\n';
}
Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
using graph = std::vector<std::vector<ll>>;

using namespace std;

vector<ll> Factorial(ll M, ll MOD) {
  vector<ll> result = vector<ll>(M + 1);
  for (int i = 0; i <= M; i++) {
    if (i == 0) {
      result[i] = 1;
    } else {
      result[i] = result[i - 1] * i % MOD;
    }
  }
  return result;
}

vector<ll> InvFact(ll M, ll MOD) {
  vector<ll> result = vector<ll>(M + 1);
  vector<ll> inv = vector<ll>(M + 1);
  result[0] = 1;
  result[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < M; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    result[i] = result[i - 1] * inv[i] % MOD;
  }
  return result;
}

ll Combination(ll n, ll k, vector<ll> fact, vector<ll> invFact, ll MOD) {
  // nCkを計算する
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fact[n] * (invFact[k] * invFact[n - k] % MOD) % MOD;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  ll MOD = 1000000007;
  vector<ll> fact = Factorial(N, MOD);
  vector<ll> invFact = InvFact(N, MOD);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    sum = (sum - A[i] * (Combination(N - (i + 1), K - 1, fact, invFact, MOD) -
                         Combination(i, K - 1, fact, invFact, MOD))) %
          MOD;
  }
  cout << sum << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
using graph = std::vector<std::vector<ll>>;

using namespace std;

vector<ll> Factorial(ll M, ll MOD) {
  vector<ll> result = vector<ll>(M + 1);
  for (int i = 0; i <= M; i++) {
    if (i == 0) {
      result[i] = 1;
    } else {
      result[i] = result[i - 1] * i % MOD;
    }
  }
  return result;
}

vector<ll> InvFact(ll M, ll MOD) {
  vector<ll> result = vector<ll>(M + 1);
  vector<ll> inv = vector<ll>(M + 1);
  result[0] = 1;
  result[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < M; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    result[i] = result[i - 1] * inv[i] % MOD;
  }
  return result;
}

ll Combination(ll n, ll k, vector<ll> &fact, vector<ll> &invFact, ll MOD) {
  // nCkを計算する
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fact[n] * (invFact[k] * invFact[n - k] % MOD) % MOD;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  ll MOD = 1000000007;
  vector<ll> fact = Factorial(N, MOD);
  vector<ll> invFact = InvFact(N, MOD);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    sum = (sum - A[i] * (Combination(N - (i + 1), K - 1, fact, invFact, MOD) -
                         Combination(i, K - 1, fact, invFact, MOD))) %
          MOD;
  }
  cout << sum << endl;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 60;

int mod = 1000000007;
const int MAX = 510000;
const int MOD = 1000000007;

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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// これを使うときにはCOMinit();を忘れずに

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  COMinit();
  int N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  ll ans = 0;
  ll acc1 = 0, acc2 = 0;
  sort(A, A + N);
  // A[i](i >= K-1)がmaxとして採用される選び方はiC(K-1)
  //(K-1)C(K-1)*A[K-1] + (K)C(K-1)*A[K] + ... + (N-1)C(K-1)*A[N-1] ...(1)
  for (int i = K - 1; i <= N - 1; i++) {
    acc1 = (acc1 + (COM(i, K - 1) * A[i] % MOD)) % MOD;
  }

  reverse(A, A + N);
  // 再度(1)を計算し,引き算 答えが負なら+MOD
  for (int i = K - 1; i <= N - 1; i++) {
    acc2 = (acc2 + (COM(i, K - 1) * A[i] % MOD)) % MOD;
    assert(acc2 >= 0);
  }
  ans = (acc1 - acc2) % MOD;

  if (ans < 0)
    ans += MOD;
  assert(ans >= 0);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 60;

int mod = 1000000007;
const int MAX = 510000;
const int MOD = 1000000007;

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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// これを使うときにはCOMinit();を忘れずに

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  COMinit();
  int N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  ll ans = 0;
  ll acc1 = 0, acc2 = 0;
  sort(A, A + N);
  // A[i](i >= K-1)がmaxとして採用される選び方はiC(K-1)
  //(K-1)C(K-1)*A[K-1] + (K)C(K-1)*A[K] + ... + (N-1)C(K-1)*A[N-1] ...(1)
  for (int i = K - 1; i <= N - 1; i++) {
    acc1 = (acc1 + (COM(i, K - 1) * A[i] % MOD)) % MOD;
  }

  reverse(A, A + N);
  // 再度(1)を計算し,引き算 答えが負なら+MOD
  for (int i = K - 1; i <= N - 1; i++) {
    acc2 = (acc2 + (COM(i, K - 1) * A[i] % MOD)) % MOD;
  }
  ans = (acc1 - acc2) % MOD;

  if (ans < 0)
    ans += MOD;
  assert(ans >= 0);
  cout << ans << endl;
  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const long long int MOD = 1e9 + 7;

long long int modinv(long long int a, long long int m) {
  long long int b = m, u = 1, v = 0;
  while (b) {
    long long int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

long long int conv(long long int i, long long int j) {
  long long int tmp = min(i - j, j);
  long long int cnt = 1;
  long long int rs = 1;
  while (cnt <= tmp) {
    rs *= (i + 1 - cnt);
    rs %= MOD;

    rs *= modinv(cnt, MOD);
    rs %= MOD;

    cnt++;
  }

  return rs;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long int> A(N);
  vector<long long int> C(N + 1, 0);
  for (int n = 0; n < N; n++) {
    cin >> A[n];
  }

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

  C[K - 1] = 1;
  for (int n = 1; n < N; n++) {
    C[K - 1 + n] = C[K - 1 + n - 1] * (K - 1 + n);
    C[K - 1 + n] %= MOD;
    C[K - 1 + n] *= modinv(n, MOD);
    C[K - 1 + n] %= MOD;
  }

  long long int ans = 0;
  for (int n = 0; n < N; n++) {
    // cout << n << ":" << endl;
    if (n - (K - 2) > 0) {
      // cout << n << "C" << K-1 << endl;
      ans += C[n] * A[n];
      ans %= MOD;
    }

    if (N - 1 - n - (K - 2) > 0) {
      // cout << N-1-n << "C" << K-1 << endl;
      ans -= C[N - 1 - n] * A[n];
      ans %= MOD;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const long long int MOD = 1e9 + 7;

long long int modinv(long long int a, long long int m) {
  long long int b = m, u = 1, v = 0;
  while (b) {
    long long int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

long long int conv(long long int i, long long int j) {
  long long int tmp = min(i - j, j);
  long long int cnt = 1;
  long long int rs = 1;
  while (cnt <= tmp) {
    rs *= (i + 1 - cnt);
    rs %= MOD;

    rs *= modinv(cnt, MOD);
    rs %= MOD;

    cnt++;
  }

  return rs;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long int> A(N);
  vector<long long int> C(N + K, 0);
  for (int n = 0; n < N; n++) {
    cin >> A[n];
  }

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

  C[K - 1] = 1;
  for (int n = 1; n < N; n++) {
    C[K - 1 + n] = C[K - 1 + n - 1] * (K - 1 + n);
    C[K - 1 + n] %= MOD;
    C[K - 1 + n] *= modinv(n, MOD);
    C[K - 1 + n] %= MOD;
  }

  long long int ans = 0;
  for (int n = 0; n < N; n++) {
    // cout << n << ":" << endl;
    if (n - (K - 2) > 0) {
      // cout << n << "C" << K-1 << endl;
      ans += C[n] * A[n];
      ans %= MOD;
    }

    if (N - 1 - n - (K - 2) > 0) {
      // cout << N-1-n << "C" << K-1 << endl;
      ans -= C[N - 1 - n] * A[n];
      ans %= MOD;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 1e5 + 11;
const int mod = 1e9 + 7;
int n, K;
LL sum[N], a[N];
int fac[N], inv[N];
LL C(int n, int m) { return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  cin >> n >> K;
  fac[0] = fac[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i <= 1e5; i++) {
    fac[i] = 1LL * fac[i - 1] * i % mod;
    inv[i] = mod - 1LL * mod / i * inv[mod % i] % mod;
  }
  for (int i = 2; i <= 1e5; i++)
    inv[i] = 1LL * inv[i - 1] * inv[i] % mod;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + a[i];
  if (K == 1) {
    puts("0");
    return 0;
  }
  int ans = 0;
  for (int p = 1; p <= n - 1; p++) {
    LL w = sum[n] - sum[p] - sum[n - p];
    w = (mod + w % mod) % mod;
    ans = (ans + w * C(p - 1, K - 2) % mod) % mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 1e5 + 11;
const int mod = 1e9 + 7;
int n, K;
LL sum[N], a[N];
LL fac[N], inv[N];
LL C(int n, int m) {
  if (m > n)
    return 0;
  return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
  cin >> n >> K;
  fac[0] = fac[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i <= 1e5; i++) {
    fac[i] = 1LL * fac[i - 1] * i % mod;
    inv[i] = mod - 1LL * mod / i * inv[mod % i] % mod;
  }
  for (int i = 2; i <= 1e5; i++)
    inv[i] = 1LL * inv[i - 1] * inv[i] % mod;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + a[i];
  if (K == 1) {
    puts("0");
    return 0;
  }
  int ans = 0;
  for (int p = 1; p <= n - 1; p++) {
    LL w = sum[n] - sum[p] - sum[n - p];
    w = (mod + w % mod) % mod;
    ans = (ans + w * C(p - 1, K - 2) % mod) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
const long long mod = 1000000007;
// const long long mod = 998244353;
class Combination {
  int MAX;
  int mod;
  long long *kai;
  long long modpow(long long a, long long n) {
    long long res = 1;
    while (n > 0) {
      if (n & 1)
        res = res * a % mod;
      a = a * a % mod;
      n >>= 1;
    }
    return res;
  }
  long long modinv(long long a) { return modpow(a, mod - 2); }
  void Init() {
    for (int i = 1; i < MAX; i++) {
      kai[i] = kai[i - 1] * i;
      kai[i] %= mod;
    }
  }

public:
  Combination(int max, int mod) : mod(mod), MAX(max + 5) {
    kai = new long long[max + 5]{1};
    Init();
  }
  ~Combination() { delete[] kai; }
  long long operator()(int n, int k) {
    if (n < 0 || k < 0)
      return 0;
    long long res = kai[n];
    res *= modinv(kai[k]);
    res %= mod;
    res *= modinv(kai[n - k]);
    res %= mod;
    // printf("%d C %d = %d\n", n, k, res);
    return res;
  }
};
int main() {
  int N, K;
  scanf("%d %d", &N, &K);
  Combination nck(N, mod);
  int *A = new int[N];
  for (int i = 0; i < N; i++)
    scanf("%d", A + i);
  std::sort(A, A + N);
  long long res = 0;
  for (int i = 0; i < N - 1; i++) {
    res += (A[N - 1 - i] * nck(N - i - 1, K - 1));
    res %= mod;
    res += mod - (A[i] * nck(N - i - 1, K - 1)) % mod;
    res %= mod;
  }
  printf("%lld", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
const long long mod = 1000000007;
// const long long mod = 998244353;
class Combination {
  int MAX;
  int mod;
  long long *kai;
  long long modpow(long long a, long long n) {
    long long res = 1;
    while (n > 0) {
      if (n & 1)
        res = res * a % mod;
      a = a * a % mod;
      n >>= 1;
    }
    return res;
  }
  long long modinv(long long a) { return modpow(a, mod - 2); }
  void Init() {
    for (int i = 1; i < MAX; i++) {
      kai[i] = kai[i - 1] * i;
      kai[i] %= mod;
    }
  }

public:
  Combination(int max, int mod) : mod(mod), MAX(max + 5) {
    kai = new long long[max + 5]{1};
    Init();
  }
  ~Combination() { delete[] kai; }
  long long operator()(int n, int k) {
    if (n < 0 || k < 0 || n < k)
      return 0;
    long long res = kai[n];
    res *= modinv(kai[k]);
    res %= mod;
    res *= modinv(kai[n - k]);
    res %= mod;
    // printf("%d C %d = %d\n", n, k, res);
    return res;
  }
};
int main() {
  int N, K;
  scanf("%d %d", &N, &K);
  Combination nck(N, mod);
  int *A = new int[N];
  for (int i = 0; i < N; i++)
    scanf("%d", A + i);
  std::sort(A, A + N);
  long long res = 0;
  for (int i = 0; i < N - 1; i++) {
    res += (A[N - 1 - i] * nck(N - i - 1, K - 1));
    res %= mod;
    res += mod - (A[i] * nck(N - i - 1, K - 1)) % mod;
    res %= mod;
  }
  printf("%lld", res);
  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vl dy = {1, -1, 0, 1, -1, 1, 0, -1};
const ll MAX = 51000;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll 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;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  COMinit();
  ll ans = 0;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n - k + 1) {
    ans -= a[i] * COM(n - 1 - i, k - 1);
    ans %= MOD;
  }
  ans = (ans + MOD) % MOD;
  reverse(a.begin(), a.end());
  rep(i, n - k + 1) {
    ans += a[i] * COM(n - 1 - i, k - 1);
    ans %= MOD;
  }
  cout << (ans + MOD) % MOD << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vl dy = {1, -1, 0, 1, -1, 1, 0, -1};
const ll MAX = 114514;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll 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;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  COMinit();
  ll ans = 0;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n - k + 1) {
    ans -= a[i] * COM(n - 1 - i, k - 1);
    ans %= MOD;
  }
  ans = (ans + MOD) % MOD;
  reverse(a.begin(), a.end());
  rep(i, n - k + 1) {
    ans += a[i] * COM(n - 1 - i, k - 1);
    ans %= MOD;
  }
  cout << (ans + MOD) % MOD << endl;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

template <typename T>
constexpr T modpow(T a, T n, T mod = 1000000007) { //(a^n)%MOD
  T ret = 1;
  while (n > 0) {
    if ((n & 1) != 0) { // n%2==1
      ret = ret * a % mod;
    }
    a = a * a % mod;
    n = n / 2;
  }
  return ret;
}

std::vector<long long> fac;
std::vector<long long> ifac;

template <typename T> T modcomb(T a, T b, T MOD = 1000000007) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  long long tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void combinit(int maxn) {
  long long MOD = 1000000007;
  fac.resize(maxn);
  ifac.resize(maxn);
  fac[0] = 1;
  ifac[0] = 1;
  for (long long i = 0; i < maxn; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;
    ifac[i + 1] = ifac[i] * modpow(i + 1, MOD - 2) % MOD;
  }
}

using namespace std;
using ll = long long;

int main() {
  ll MOD = 1000000007;
  ll n, k;
  cin >> n >> k;
  combinit(n);
  vector<ll> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ll r = n - 1 - i;
    ll l = i;
    ll cnt = modcomb(l, k - 1);
    cnt -= modcomb(r, k - 1);
    if (cnt < 0)
      cnt += MOD;
    if (a[i] < 0)
      a[i] += MOD;
    ans += (a[i] * cnt) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

template <typename T>
constexpr T modpow(T a, T n, T mod = 1000000007) { //(a^n)%MOD
  T ret = 1;
  while (n > 0) {
    if ((n & 1) != 0) { // n%2==1
      ret = ret * a % mod;
    }
    a = a * a % mod;
    n = n / 2;
  }
  return ret;
}

std::vector<long long> fac;
std::vector<long long> ifac;

template <typename T> T modcomb(T a, T b, T MOD = 1000000007) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  long long tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void combinit(int maxn) {
  long long MOD = 1000000007;
  fac.resize(maxn + 1);
  ifac.resize(maxn + 1);
  fac[0] = 1;
  ifac[0] = 1;
  for (long long i = 0; i < maxn; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;
    ifac[i + 1] = ifac[i] * modpow(i + 1, MOD - 2) % MOD;
  }
}

using namespace std;
using ll = long long;

int main() {
  ll MOD = 1000000007;
  ll n, k;
  cin >> n >> k;
  combinit(n);
  vector<ll> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ll r = n - 1 - i;
    ll l = i;
    ll cnt = modcomb(l, k - 1);
    cnt -= modcomb(r, k - 1);
    if (cnt < 0)
      cnt += MOD;
    if (a[i] < 0)
      a[i] += MOD;
    ans += (a[i] * cnt) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ll long long

#define MOD 1000000007

using namespace std;

ll facs[(ll)1e5 + 10];

// x^n in mod
ll modpow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;

  if (n % 2 == 0) {
    return modpow(x * x % mod, n / 2, mod) % mod;
  } else {
    return x * modpow(x, (n - 1), mod) % mod;
  }
}

// n! in mod
ll fac(ll n) { return facs[n]; }

// nCr in mod
ll comb(ll n, ll r) {
  if (n < 0 || n < r || r < 0)
    return 0;
  ll fn = fac(n);
  ll fr = fac(r);
  ll fn_r = fac(n - r);

  ll d = (modpow(fr, MOD - 2, MOD) * modpow(fn_r, MOD - 2, MOD)) % MOD;
  ll u = fn % MOD;

  ll result = u * d % MOD;

  return result;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

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

  facs[0] = 1;
  rep(i, n + 10) { facs[i + 1] = (facs[i] * (i + 1)) % MOD; }

  ll result = 0;
  rep(i, n - k + 1) {
    ll cc = comb(n - 1 - i, k - 1);

    result += cc * a[n - 1 - i];
    result %= MOD;

    result -= cc * a[i];
    while (result < 0)
      result += MOD;
    result %= MOD;
  }

  cout << result;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ll long long

#define MOD 1000000007

using namespace std;

ll facs[(ll)1e5 + 10];

// x^n in mod
ll modpow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;

  if (n % 2 == 0) {
    return modpow(x * x % mod, n / 2, mod) % mod;
  } else {
    return x * modpow(x, (n - 1), mod) % mod;
  }
}

// n! in mod
ll fac(ll n) { return facs[n]; }

// nCr in mod
ll comb(ll n, ll r) {
  if (n < 0 || n < r || r < 0)
    return 0;
  ll fn = fac(n);
  ll fr = fac(r);
  ll fn_r = fac(n - r);

  ll d = (modpow(fr, MOD - 2, MOD) * modpow(fn_r, MOD - 2, MOD)) % MOD;
  ll u = fn % MOD;

  ll result = u * d % MOD;

  return result;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

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

  facs[0] = 1;
  rep(i, n + 10) { facs[i + 1] = (facs[i] * (i + 1)) % MOD; }

  ll result = 0;
  rep(i, n - k + 1) {
    ll cc = comb(n - 1 - i, k - 1);

    result += cc * a[n - 1 - i];
    result %= MOD;

    result -= cc * a[i];
    result += MOD;
    result += MOD;
    result %= MOD;
  }

  cout << result;

  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

ll mod = 1e9 + 7;

ll com(ll N, ll K, ll L) {
  ll d = 1, e = (L * K) % mod, m_ = mod - 2;
  while (m_ != 0) {
    if (m_ & 1)
      d = (d * e) % mod;
    e = e * e % mod;
    m_ = m_ >> 1;
  }
  return (N * d) % mod;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, ans = 0;
  cin >> n >> k;
  lvector A(n, 0), mf(n + 1, 0);
  rep(i, n) cin >> A[i];
  sort(ALL(A));
  mf[0] = 1;
  for (ll i = 1; i <= n; ++i)
    mf[i] = (mf[i - 1] * i) % mod;
  for (ll pos = 0; pos < n; pos++) {
    if (n - pos >= k)
      ans -= A[pos] * com(mf[n - 1 - pos], mf[k - 1], mf[n - k - pos]);
    while (ans < 0)
      ans += mod;
    if (pos >= k - 1)
      ans += A[pos] * com(mf[pos], mf[k - 1], mf[pos + 1 - k]);
    ans %= mod;
  }
  print(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

ll mod = 1e9 + 7;

ll com(ll N, ll K, ll L) {
  ll d = 1, e = (L * K) % mod, m_ = mod - 2;
  while (m_ != 0) {
    if (m_ & 1)
      d = (d * e) % mod;
    e = e * e % mod;
    m_ = m_ >> 1;
  }
  return (N * d) % mod;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, ans = 0;
  cin >> n >> k;
  lvector A(n, 0), mf(n + 1, 0);
  rep(i, n) cin >> A[i];
  sort(ALL(A));
  mf[0] = 1;
  for (ll i = 1; i <= n; ++i)
    mf[i] = (mf[i - 1] * i) % mod;
  for (ll pos = 0; pos <= n - k; ++pos)
    ans =
        (ans - A[pos] * com(mf[n - 1 - pos], mf[k - 1], mf[n - k - pos])) % mod;
  for (ll pos = k - 1; pos < n; ++pos)
    ans += (A[pos] * com(mf[pos], mf[k - 1], mf[pos + 1 - k])) % mod;
  ans %= mod;
  while (ans < 0)
    ans += mod;
  print(ans);
  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 3, MOD = 1e9 + 7;
long long f[N], cumNCK[N];
long long power(int b, int e) {
  long long ret = 1;
  while (e) {
    while (e % 2 == 0) {
      e /= 2;
      b = (1LL * b * b) % MOD;
    }
    --e;
    ret = (ret * b) % MOD;
  }
  return ret;
}
long long nCr(int n, int r) {
  assert(n >= r);
  return (f[n] * power((f[r] * f[n - r]) % MOD, MOD - 2)) % MOD;
}
int n, k, a[N];
int main() {
  //	freopen("input.in", "r", stdin);
  f[0] = 1;
  for (int i = 1; i < N; ++i)
    f[i] = (f[i - 1] * i) % MOD;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  if (k == 1) {
    puts("0");
    return 0;
  }
  sort(a, a + n);
  for (int i = 0; i <= n * 2; ++i) {
    if (k - 2 <= i) {
      if (i)
        cumNCK[i] = (cumNCK[i - 1] + nCr(i, k - 2)) % MOD;
      else
        cumNCK[i] = nCr(i, k - 2);
      //			cout << nCr(i, k - 2) << " " << cumNCK[i] <<
      //endl;
    }
  }
  long long res = 0;
  for (int i = n - 1; i >= 0; --i) {
    int len = i - k + 2;
    //		cout << a[i] << " " << len << ", ";
    if (len >= 0) {
      res = (res + a[i] * cumNCK[len + k - 2 - 1]) % MOD;
    }
    //		cout << endl;
    len = n - (i + k) + 1;
    if (len >= 0) {
      res = ((res - a[i] * cumNCK[len + k - 2 - 1]) % MOD + MOD) % MOD;
      //			cout << a[i] << " " << len << endl;
    }
  }
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 3, MOD = 1e9 + 7;
long long f[N], cumNCK[N];
long long power(int b, int e) {
  long long ret = 1;
  while (e) {
    while (e % 2 == 0) {
      e /= 2;
      b = (1LL * b * b) % MOD;
    }
    --e;
    ret = (ret * b) % MOD;
  }
  return ret;
}
long long nCr(int n, int r) {
  assert(n >= r);
  return (f[n] * power((f[r] * f[n - r]) % MOD, MOD - 2)) % MOD;
}
int n, k, a[N];
int main() {
  //	freopen("input.in", "r", stdin);
  f[0] = 1;
  for (int i = 1; i < N; ++i)
    f[i] = (f[i - 1] * i) % MOD;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  if (k == 1) {
    puts("0");
    return 0;
  }
  sort(a, a + n);
  for (int i = 0; i <= n * 2; ++i) {
    if (k - 2 <= i) {
      if (i)
        cumNCK[i] = (cumNCK[i - 1] + nCr(i, k - 2)) % MOD;
      else
        cumNCK[i] = nCr(i, k - 2);
      //			cout << nCr(i, k - 2) << " " << cumNCK[i] <<
      //endl;
    }
  }
  long long res = 0;
  for (int i = n - 1; i >= 0; --i) {
    int len = i - k + 2;
    //		cout << a[i] << " " << len << ", ";
    if (len >= 0) {
      res = (res + a[i] * cumNCK[len + k - 2 - 1]) % MOD;
    }
    //		cout << endl;
    len = n - (i + k) + 1;
    if (len >= 0) {
      res = ((res - a[i] * cumNCK[len + k - 2 - 1]) % MOD + MOD) % MOD;
      //			cout << a[i] << " " << len << endl;
    }
  }
  printf("%lld\n", res);
  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ull long long

using namespace std;
long long fac[100005];
long long infact[100005];

ull mul(ull a, ull b, ull c) {
  ull dev = 0, tmp = a % c;
  while (b) {
    if (b & 1)
      if ((dev += tmp) >= c)
        dev -= c;
    if ((tmp <<= 1) >= c)
      tmp -= c;
    b >>= 1;
  }
  return dev;
}
ull exp(ull a, ull b, ull c) {
  if (b == 0)
    return 1;
  ull dev = exp(a, b >> 1, c);
  dev = mul(dev, dev, c);
  if (b & 1)
    dev = mul(dev, a, c);
  return dev;
}

// Returns n^(-1) mod p
int modInverse(int n, int p) { return exp(n, p - 2, p); }

// Returns nCr % p using Fermat's little
// theorem.
int nCrModP(int n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  return mul(fac[n], mul(infact[r], infact[n - r], p), p);
}

int main() {
  int p = 1000000007;
  fac[0] = 1;
  infact[0] = 1;
  for (int i = 1; i <= 100001; i++) {
    fac[i] = (fac[i - 1] * i) % p;
    infact[i] = modInverse(fac[i], p);
  }
  int n, k;
  cin >> n >> k;

  vector<int> a(n);

  for (int &i : a)
    cin >> i;

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

  long long ans = 0;

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

    if (i >= k - 1) {
      ans += mul(nCrModP(i, k - 1, p), a[i], p);
    }

    ans %= p;

    if (n - i - 1 >= k - 1) {
      ans -= mul(nCrModP(n - i - 1, k - 1, p), a[i], p);
    }

    ans %= p;
  }

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ull long long

using namespace std;
long long fac[100005];
long long infact[100005];

ull mul(ull a, ull b, ull c) { return ((a % c) * (b % c)) % c; }
ull exp(ull a, ull b, ull c) {
  if (b == 0)
    return 1;
  ull dev = exp(a, b >> 1, c);
  dev = mul(dev, dev, c);
  if (b & 1)
    dev = mul(dev, a, c);
  return dev;
}

// Returns n^(-1) mod p
int modInverse(int n, int p) { return exp(n, p - 2, p); }

// Returns nCr % p using Fermat's little
// theorem.
int nCrModP(int n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  return mul(fac[n], mul(infact[r], infact[n - r], p), p);
}

int main() {
  int p = 1000000007;
  fac[0] = 1;
  infact[0] = 1;
  for (int i = 1; i <= 100001; i++) {
    fac[i] = (fac[i - 1] * i) % p;
    infact[i] = modInverse(fac[i], p);
  }
  int n, k;
  cin >> n >> k;

  vector<int> a(n);

  for (int &i : a)
    cin >> i;

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

  long long ans = 0;

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

    if (i >= k - 1) {
      ans += mul(nCrModP(i, k - 1, p), a[i], p);
    }

    ans %= p;

    if (n - i - 1 >= k - 1) {
      ans -= mul(nCrModP(n - i - 1, k - 1, p), a[i], p);
    }

    ans %= p;
  }

  cout << ans << '\n';

  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):

// Problem : E - Max-Min Sums
// Contest : AtCoder Beginner Contest 151
// URL : https://atcoder.jp/contests/abc151/tasks/abc151_e
// Memory Limit : 1024.000000 MB
// Time Limit : 2000.000000 milisec
// Powered by CP Editor (https://github.com/coder3101/cp-editor)

#include <bits/stdc++.h>

using namespace std;

int N, K;
long long MOD = 1e9 + 7;
long long arr[1000005];
long long fact[1000005];

long long mult(long long a, long long b) { return (a * b) % MOD; }

long long add(long long a, long long b) { return (a + b) % MOD; }

long long qikpow(long long b, long long e) {
  if (!e) {
    return 1;
  }
  long long ret = qikpow(b, e / 2);
  ret = mult(ret, ret);
  if (e & 1) {
    ret = mult(ret, b);
  }
  return ret;
}

long long divd(long long a, long long b) { return mult(a, qikpow(b, MOD - 2)); }

long long sub(long long a, long long b) {
  a = add(a, -b);
  if (a < 0) {
    a += MOD;
  }
  return a;
}

long long choose(long long n, long long r) {
  return divd(fact[n], mult(fact[r], fact[n - r]));
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> K;
  fact[0] = 1;
  for (int i = 1; i <= N; i++) {
    fact[i] = mult(i, fact[i - 1]);
    cin >> arr[i];
  }
  sort(arr + 1, arr + 1 + N);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = add(ans, mult(arr[i], choose(i - 1, K - 1)));
    ans = sub(ans, mult(arr[i], choose(N - i, K - 1)));
  }
  cout << ans << "\n";
}

Fixed submission:

// Problem : E - Max-Min Sums
// Contest : AtCoder Beginner Contest 151
// URL : https://atcoder.jp/contests/abc151/tasks/abc151_e
// Memory Limit : 1024.000000 MB
// Time Limit : 2000.000000 milisec
// Powered by CP Editor (https://github.com/coder3101/cp-editor)

#include <bits/stdc++.h>

using namespace std;

int N, K;
long long MOD = 1e9 + 7;
long long arr[1000005];
long long fact[1000005];

long long mult(long long a, long long b) { return (a * b) % MOD; }

long long add(long long a, long long b) { return (a + b) % MOD; }

long long qikpow(long long b, long long e) {
  if (!e) {
    return 1;
  }
  long long ret = qikpow(b, e / 2);
  ret = mult(ret, ret);
  if (e & 1) {
    ret = mult(ret, b);
  }
  return ret;
}

long long divd(long long a, long long b) { return mult(a, qikpow(b, MOD - 2)); }

long long sub(long long a, long long b) {
  a = add(a, -b);
  if (a < 0) {
    a += MOD;
  }
  return a;
}

long long choose(long long n, long long r) {
  if (n - r < 0) {
    return 0;
  }
  return divd(fact[n], mult(fact[r], fact[n - r]));
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> K;
  fact[0] = 1;
  for (int i = 1; i <= N; i++) {
    fact[i] = mult(i, fact[i - 1]);
    cin >> arr[i];
  }
  sort(arr + 1, arr + 1 + N);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = add(ans, mult(arr[i], choose(i - 1, K - 1)));
    ans = sub(ans, mult(arr[i], choose(N - i, K - 1)));
  }
  cout << ans << "\n";
}
Problem p02804 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

inv_t = [0, 1] + [0] * (N - 2)
for i in range(2, N):
    inv_t[i] = inv_t[MOD % i] * (MOD - MOD // i) % MOD

A.sort()
ans = 0
C = 1
for i in range(K, N + 1):
    ans += (A[i - 1] - A[N - i]) * C
    ans %= MOD
    C = C * i * inv_t[i - K + 1]
    C %= MOD
print(ans)


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

inv_t = [0, 1] + [0] * (N - 1)
for i in range(2, N + 1):
    inv_t[i] = inv_t[MOD % i] * (MOD - MOD // i) % MOD

A.sort()
ans = 0
C = 1
for i in range(K, N + 1):
    ans += (A[i - 1] - A[N - i]) * C
    ans %= MOD
    C = C * i * inv_t[i - K + 1]
    C %= MOD
print(ans)
Problem p02804 (Python)

Buggy submission (status: Time Limit Exceeded):
MOD = 10**9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


N, K = map(int, input().split())
A = list(map(int, input().split()))
modFacts, invs = prepare(N)


# mod2 = [0] * (N + 1)
# mod2[0] = 1
# for i in range(N):
#     mod2[i + 1] = mod2[i] * 2
#     mod2[i + 1] %= MOD

A.sort()
ans = 0
for i in range(N):
    for j in range(K - 1, N - i):
        x = A[i + j]
        y = A[i]
        ans += (x - y) * modFacts[j - 1] * invs[j - 1 - (K - 2)] * invs[K - 2]
        ans %= MOD

print(ans)


Fixed submission:
MOD = 10**9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


N, K = map(int, input().split())
A = list(map(int, input().split()))
modFacts, invs = prepare(N)


# mod2 = [0] * (N + 1)
# mod2[0] = 1
# for i in range(N):
#     mod2[i + 1] = mod2[i] * 2
#     mod2[i + 1] %= MOD

A.sort()
ans = 0

for i in range(K - 1, N):
    n = i
    r = K - 1
    num = (modFacts[n] * invs[n - r] * invs[r]) % MOD
    ans += A[i] * num
    ans %= MOD

for i in range(N - K + 1):
    n = N - (i + 1)
    r = K - 1
    num = (modFacts[n] * invs[n - r] * invs[r]) % MOD
    ans -= A[i] * num
    ans %= MOD


print(ans)