Language: C++

#include <array>
#include <algorithm>
#include <iostream>

template <typename T, T N>
class Modulo {
 public:
  using Type = decltype(N);
  Modulo (Type n) : number_(n % N) {}
  Modulo () : Modulo(0) {}
  auto & operator += (const Modulo & m) {
    number_ = (number_ + m.number_) % N;
    return *this;
  }
  auto & operator -= (const Modulo & m) {
    number_ = (number_ - m.number_) % N;
    return *this;
  }
  auto & operator *= (const Modulo & m) {
    number_ = (number_ * m.number_) % N;
    return *this;
  }
  auto operator ~ () {
    // N が素数であり、かつ
    // number_ が N と互いに素である場合に限る
    return power(*this, N - 2);
  }
  friend auto operator + (Modulo lhs, const Modulo & rhs) {
    return lhs += rhs;
  }
  friend auto operator - (Modulo lhs, const Modulo & rhs) {
    return lhs -= rhs;
  }
  friend auto operator * (Modulo lhs, const Modulo & rhs) {
    return lhs *= rhs;
  }
  friend auto & operator << (std::ostream & os, const Modulo & m) {
    return os << m.number_;
  }
  friend auto & operator >> (std::istream & is, Modulo & m) {
    return is >> m.number_;
  }
 private:
  Type number_;
};

template <typename T>
auto
power (
    T   base,
    int exponent)
{
    decltype(base) result = 1;

    for (; exponent; exponent >>= 1) {
      if ( exponent & 1 ) {
        result *= base;
      }
      base *= base;
    }
    return result;
}

template <typename T, std::size_t INPUT_MAX>
auto
combination (
    std::size_t n,
    std::size_t k)
{
    // assert(k <= n && n <= INPUT_MAX);
    static std::array<T, INPUT_MAX + 1> facs;
    static std::array<T, INPUT_MAX + 1> invs;
    static bool ready = false;

    if ( !ready ) {
      const auto N = INPUT_MAX;
      facs[0] = 1;
      for (auto i = 1; i <= N; ++i) {
        facs[i] = facs[i - 1] * i;
      }
      invs[N] = ~facs[N];
      for (auto i = N; 1 <= i; --i) {
        invs[i - 1] = invs[i] * i;
      }
      ready = true;
    }

    return facs[n] * invs[k] * invs[n - k];
}

int
main ()
{
    using Number = Modulo<1'000'000'007ll>;
    const auto INPUT_MAX = 100'000;
    auto c = combination<Number, INPUT_MAX>;

    int n, k;
    std::array<int, INPUT_MAX + 1> a;

    std::cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
      std::cin >> a[i];
    }
    std::sort(std::begin(a) + 1, std::begin(a) + 1 + n);

    Number maxsum, minsum;
    for (int i = n; k <= i; --i) {
      Number count = c(i - 1, k - 1);
      maxsum += count * a[i];
      minsum += count * a[n + 1 - i];
    }
    std::cout << maxsum - minsum << std::endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:94:42: error: wrong number of template arguments (1, should be 2)
   94 |     using Number = Modulo<1'000'000'007ll>;
      |                                          ^
a.cc:6:7: note: provided for 'template<class T, T N> class Modulo'
    6 | class Modulo {
      |       ^~~~~~
a.cc:96:26: error: 'Number' was not declared in this scope
   96 |     auto c = combination<Number, INPUT_MAX>;
      |                          ^~~~~~
a.cc:96:14: error: unable to deduce 'auto' from 'combination<<expression error>, INPUT_MAX>'
   96 |     auto c = combination<Number, INPUT_MAX>;
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:96:14: note:   couldn't deduce template parameter 'auto'
a.cc:107:11: error: expected ';' before 'maxsum'
  107 |     Number maxsum, minsum;
      |           ^~~~~~~
      |           ;
a.cc:109:13: error: expected ';' before 'count'
  109 |       Number count = c(i - 1, k - 1);
      |             ^~~~~~
      |             ;
a.cc:110:7: error: 'maxsum' was not declared in this scope
  110 |       maxsum += count * a[i];
      |       ^~~~~~
a.cc:110:17: error: 'count' was not declared in this scope; did you mean 'std::count'?
  110 |       maxsum += count * a[i];
      |                 ^~~~~
      |                 std::count
In file included from /usr/include/c++/14/algorithm:86,
                 from a.cc:2:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:101:1: note: 'std::count' declared here
  101 | count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
      | ^~~~~
a.cc:111:7: error: 'minsum' was not declared in this scope
  111 |       minsum += count * a[n + 1 - i];
      |       ^~~~~~
a.cc:113:18: error: 'maxsum' was not declared in this scope
  113 |     std::cout << maxsum - minsum << std::endl;
      |                  ^~~~~~
a.cc:113:27: error: 'minsum' was not declared in this scope
  113 |     std::cout << maxsum - minsum << std::endl;
      |                           ^~~~~~