Language: C++

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include<iostream>
#include<map>
#include <algorithm>
#include <vector>

using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
#define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using P = pair<i64, i64>;


constexpr i64 MAX = 10000000;
constexpr i64 MOD = 1000000007;
i64 fac[MAX], finv[MAX], inv[MAX];

template<i64 modulus>
class modcal {

public:
    i64 a;

    constexpr modcal(const i64 x = 0) noexcept: a(x % modulus) {}

    constexpr i64 &value() noexcept { return a; }

    constexpr const i64 &value() const noexcept { return a; }

    constexpr modcal operator+(const modcal rhs) const noexcept {
        return modcal(*this) += rhs;
    }

    constexpr modcal operator-(const modcal rhs) const noexcept {
        return modcal(*this) -= rhs;
    }

    constexpr modcal operator*(const modcal rhs) const noexcept {
        return modcal(*this) *= rhs;
    }

    constexpr modcal operator/(const modcal rhs) noexcept {
        return modcal(*this) /= rhs;
    }

    constexpr modcal &operator+=(const modcal rhs) noexcept {
        a += rhs.a;
        if (a >= modulus) {
            a -= modulus;
        }
        return *this;
    }

    constexpr modcal &operator-=(const modcal rhs) noexcept {
        if (a < rhs.a) {
            a += modulus;
        }
        a -= rhs.a;
        return *this;
    }

    constexpr modcal &operator*=(const modcal rhs) noexcept {
        a = a * rhs.a % modulus;
        return *this;
    }

    constexpr modcal &operator/=(modcal rhs) noexcept {
        i64 exp = modulus - 2;
        while (exp) {
            if (exp % 2) {
                *this *= rhs;
            }
            rhs *= rhs;
            exp /= 2;
        }
        return *this;
    }

    void COMninit() {
        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 % modulus;
            inv[i] = MOD - inv[modulus % i] * (modulus / i) % modulus;
            finv[i] = finv[i - 1] * inv[i] % modulus;
        }
    }

    i64 COMn(i64 n, i64 k) {
        if (n < k)
            return 0;
        if (n < 0 || k < 0)
            return 0;
        return fac[n] * (finv[k] * finv[n - k] % modulus) % modulus;
    }

    constexpr modcal<1000000007> modpow(const modcal<1000000007> &a, i64 n) {
        if (n == 0)
            return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1)
            t = t * a;
        return t;
    }
};

using modc = modcal<1000000007>;

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    i32 n, k;
    cin >> n >> k;
    vector<i32> a(n);
    map<i32,i32> mp;
    rep(i,n) {
        cin >> a[i];
        mp[a[i]]++;
    }
    sort(all(a));
    modc ans = 0;
    ans.COMninit();
    map<i32,i32> mp_max,mp_min;
    rep(i,k + 1){
            mp_min[a[i]] += ans.COMn(n - i - 1,k - 1);
    }
    sort(rall(a));
    rep(i,k + 1){
            mp_max[a[i]] += ans.COMn(n - i - 1,k - 1);
    }
    vector<P> maxes,mines;
    for(auto v:mp_max){
        maxes.emplace_back(v);
    }
    for(auto v:mp_min){
        mines.emplace_back(v);
    }
    for(auto v:mp_max){
        ans += v.first * v.second;
    }
    for(auto v:mp_min){
        ans -= v.first * .second;
    }
    cout << ans.a << endl;
}

Compiler output:
a.cc:11:13: error: 'int_fast32_t' does not name a type
   11 | using i32 = int_fast32_t;
      |             ^~~~~~~~~~~~
a.cc:12:13: error: 'int_fast64_t' does not name a type
   12 | using i64 = int_fast64_t;
      |             ^~~~~~~~~~~~
a.cc:16:16: error: 'i64' was not declared in this scope
   16 | using P = pair<i64, i64>;
      |                ^~~
a.cc:16:21: error: 'i64' was not declared in this scope
   16 | using P = pair<i64, i64>;
      |                     ^~~
a.cc:16:24: error: template argument 1 is invalid
   16 | using P = pair<i64, i64>;
      |                        ^
a.cc:16:24: error: template argument 2 is invalid
a.cc:19:11: error: 'i64' does not name a type
   19 | constexpr i64 MAX = 10000000;
      |           ^~~
a.cc:20:11: error: 'i64' does not name a type
   20 | constexpr i64 MOD = 1000000007;
      |           ^~~
a.cc:21:1: error: 'i64' does not name a type
   21 | i64 fac[MAX], finv[MAX], inv[MAX];
      | ^~~
a.cc:23:10: error: 'i64' has not been declared
   23 | template<i64 modulus>
      |          ^~~
a.cc:27:5: error: 'i64' does not name a type
   27 |     i64 a;
      |     ^~~
a.cc:29:28: error: 'i64' does not name a type
   29 |     constexpr modcal(const i64 x = 0) noexcept: a(x % modulus) {}
      |                            ^~~
a.cc:31:15: error: 'i64' does not name a type
   31 |     constexpr i64 &value() noexcept { return a; }
      |               ^~~
a.cc:33:21: error: 'i64' does not name a type
   33 |     constexpr const i64 &value() const noexcept { return a; }
      |                     ^~~
a.cc:95:5: error: 'i64' does not name a type
   95 |     i64 COMn(i64 n, i64 k) {
      |     ^~~
a.cc:103:70: error: 'i64' has not been declared
  103 |     constexpr modcal<1000000007> modpow(const modcal<1000000007> &a, i64 n) {
      |                                                                      ^~~
a.cc: In function 'int main()':
a.cc:119:5: error: 'i32' was not declared in this scope
  119 |     i32 n, k;
      |     ^~~
a.cc:120:12: error: 'n' was not declared in this scope
  120 |     cin >> n >> k;
      |            ^
a.cc:120:17: error: 'k' was not declared in this scope
  120 |     cin >> n >> k;
      |                 ^
a.cc:121:15: error: template argument 2 is invalid
  121 |     vector<i32> a(n);
      |               ^
a.cc:122:16: error: template argument 3 is invalid
  122 |     map<i32,i32> mp;
      |                ^
a.cc:122:16: error: template argument 4 is invalid
a.cc:123:9: error: expected ';' before 'i'
  123 |     rep(i,n) {
      |         ^
a.cc:13:28: note: in definition of macro 'rep'
   13 | #define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
      |                            ^
a.cc:123:9: error: 'i' was not declared in this scope
  123 |     rep(i,n) {
      |         ^
a.cc:13:35: note: in definition of macro 'rep'
   13 | #define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
      |                                   ^
a.cc:14:20: error: request for member 'begin' in 'a', which is of non-class type 'int'
   14 | #define all(a) (a).begin(),(a).end()
      |                    ^~~~~
a.cc:127:10: note: in expansion of macro 'all'
  127 |     sort(all(a));
      |          ^~~
a.cc:14:32: error: request for member 'end' in 'a', which is of non-class type 'int'
   14 | #define all(a) (a).begin(),(a).end()
      |                                ^~~
a.cc:127:10: note: in expansion of macro 'all'
  127 |     sort(all(a));
      |          ^~~
a.cc:128:5: error: 'modc' was not declared in this scope; did you mean 'modcal'?
  128 |     modc ans = 0;
      |     ^~~~
      |     modcal
a.cc:129:5: error: 'ans' was not declared in this scope; did you mean 'abs'?
  129 |     ans.COMninit();
      |     ^~~
      |     abs
a.cc:130:16: error: template argument 3 is invalid
  130 |     map<i32,i32> mp_max,mp_min;
      |                ^
a.cc:130:16: error: template argument 4 is invalid
a.cc:131:9: error: expected ';' before 'i'
  131 |     rep(i,k + 1){
      |         ^
a.cc:13:28: note: in definition of macro 'rep'
   13 | #define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
      |                            ^
a.cc:131:9: error: 'i' was not declared in this scope
  131 |     rep(i,k + 1){
      |         ^
a.cc:13:35: note: in definition of macro 'rep'
   13 | #define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
      |                                   ^
a.cc:15:21: error: request for member 'rbegin' in 'a', which is of non-class type 'int'
   15 | #define rall(a) (a).rbegin(),(a).rend()
      |                     ^~~~~~
a.cc:134:10: note: in expansion of macro 'rall'
  134 |     sort(rall(a));
      |          ^~~~
a.cc:15:34: error: request for member 'rend' in 'a', which is of non-class type 'int'
   15 | #define rall(a) (a).rbegin(),(a).rend()
      |                                  ^~~~
a.cc:134:10: note: in expansion of macro 'rall'
  134 |     sort(rall(a));
      |          ^~~~
a.cc:135:9: error: expected ';' before 'i'
  135 |     rep(i,k + 1){
      |         ^
a.cc:13:28: note: in definition of macro 'rep'
   13 | #define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
      |                            ^
a.cc:135:9: error: 'i' was not declared in this scope
  135 |     rep(i,k + 1){
      |         ^
a.cc:13:35: note: in definition of macro 'rep'
   13 | #define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
      |                                   ^
a.cc:138:12: error: 'P' was not declared in this scope
  138 |     vector<P> maxes,mines;
      |            ^
a.cc:138:13: error: template argument 1 is invalid
  138 |     vector<P> maxes,mines;
      |             ^
a.cc:138:13: error: template argument 2 is invalid
a.cc:139:16: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
  139 |     for(auto v:mp_max){
      |                ^~~~~~
      |                std::begin
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:5:
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:139:16: error: 'end' was not declared in this scope; did you mean 'std::end'?
  139 |     for(auto v:mp_max){
      |                ^~~~~~
      |                std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:140:15: error: request for member 'emplace_back' in 'maxes', which is of non-class type 'int'
  140 |         maxes.emplace_back(v);
      |               ^~~~~~~~~~~~
a.cc:142:16: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
  142 |     for(auto v:mp_min){
      |                ^~~~~~
      |                std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:142:16: error: 'end' was not declared in this scope; did you mean 'std::end'?
  142 |     for(auto v:mp_min){
      |                ^~~~~~
      |                std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:143:15: error: request for member 'emplace_back' in 'mines', which is of non-class type 'int'
  143 |         mines.emplace_back(v);
      |               ^~~~~~~~~~~~
a.cc:145:16: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
  145 |     for(auto v:mp_max){
      |                ^~~~~~
      |                std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:145:16: error: 'end' was not declared in this scope; did you mean 'std::end'?
  145 |     for(auto v:mp_max){
      |                ^~~~~~
      |                std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:148:16: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
  148 |     for(auto v:mp_min){
      |                ^~~~~~
      |                std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:148:16: error: 'end' was not declared in this scope; did you mean 'std::end'?
  148 |     for(auto v:mp_min){
      |                ^~~~~~
      |                std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:149:26: error: expected primary-expression before '.' token
  149 |         ans -= v.first * .second;
      |                          ^