Language: C++

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

template <auto 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 'auto combination(std::size_t, std::size_t)':
a.cc:70:5: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   70 |     assert(k <= n && n <= INPUT_MAX);
      |     ^~~~~~
a.cc:70:5: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: In instantiation of 'auto combination(std::size_t, std::size_t) [with T = Modulo<1000000007>; long unsigned int INPUT_MAX = 100000; std::size_t = long unsigned int]':
a.cc:96:14:   required from here
   96 |     auto c = combination<Number, INPUT_MAX>;
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:70:11: error: 'assert' was not declared in this scope
   70 |     assert(k <= n && n <= INPUT_MAX);
      |     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:4:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    3 | #include <iostream>
  +++ |+#include <cassert>
    4 |