Language: C++

#include <bits/stdc++.h>
using namespace std;

// using ll = long long;
#define int long long

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for(int i = (n); i >= 0; --i) // from n to 0
#define REPRS(i, r, n) for(int i = (n); i >= (r); --i) // from n to r
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))

int Lcm(int, int);
bool IsPrime(int);
ll Factorial(int);
ll NPR (int, int);
ll NCR (int, int);

// signed com[100005][100005] = {};

/***** MAIN *****/
signed main() {
  int n,k;
  cin >> n >> k;
  vector<int> nums(n);
  REP(i,n) {
    cin >> nums[i];
  }
  SORT(nums);
  ll ans = 0;
  if(k==1) {
    cout << 0 << endl;
    return 0;
  }
  REP(left_i,n-k+1) {
    REPS(right_i, left_i+k-1, n) {
      ll diff = nums[right_i] - nums[left_i];
      if(k==2) {
        ans += diff;
        continue;
      }
      int options = right_i - left_i - 1;
      if(options <= 1) ans += diff;
      else {
        ans += diff * NCR(options, k-2);
      }
    }
  }
  cout << ans % MOD;


  
  cout << "\n";
  return 0;
}
/***** MAIN *****/

int Lcm(int a, int b) {
  int bigger = max(a,b);
  int smaller = min(a,b);
  int ans = bigger;
  while(ans%smaller!=0) ans+=bigger;
  return ans;
}

bool IsPrime(int a) {
  if(a <= 1) return false;
  if(a == 2) return true;
  if(a%2 == 0) return false;
  for(int i = 3; i*i <= a; i+=2) {
    if(a%i==0) return false;
  }
  return true;
}

ll Factorial(int n){
  ll ans = 1;
  while(n > 1){
    ans *= n;
    --n;
  }
  return ans;
}

ll NPR(int n, int r) {
  return Factorial(n)/Factorial(n-r);
}

ll NCR (int n, int r){
  return NPR(n,r)/Factorial(r);
  // if(com[n][r] > 0) return com[n][r];
  // com[n][r] = NPR(n,r)/Factorial(r);
  // return com[n][r];
}


Compiler output:
a.cc:22:1: error: 'll' does not name a type
   22 | ll Factorial(int);
      | ^~
a.cc:23:1: error: 'll' does not name a type
   23 | ll NPR (int, int);
      | ^~
a.cc:24:1: error: 'll' does not name a type
   24 | ll NCR (int, int);
      | ^~
a.cc: In function 'int main()':
a.cc:37:3: error: 'll' was not declared in this scope
   37 |   ll ans = 0;
      |   ^~
a.cc:44:9: error: expected ';' before 'diff'
   44 |       ll diff = nums[right_i] - nums[left_i];
      |         ^~~~~
      |         ;
a.cc:46:9: error: 'ans' was not declared in this scope; did you mean 'abs'?
   46 |         ans += diff;
      |         ^~~
      |         abs
a.cc:46:16: error: 'diff' was not declared in this scope
   46 |         ans += diff;
      |                ^~~~
a.cc:50:24: error: 'ans' was not declared in this scope; did you mean 'abs'?
   50 |       if(options <= 1) ans += diff;
      |                        ^~~
      |                        abs
a.cc:50:31: error: 'diff' was not declared in this scope
   50 |       if(options <= 1) ans += diff;
      |                               ^~~~
a.cc:52:9: error: 'ans' was not declared in this scope; did you mean 'abs'?
   52 |         ans += diff * NCR(options, k-2);
      |         ^~~
      |         abs
a.cc:52:16: error: 'diff' was not declared in this scope
   52 |         ans += diff * NCR(options, k-2);
      |                ^~~~
a.cc:52:23: error: 'NCR' was not declared in this scope
   52 |         ans += diff * NCR(options, k-2);
      |                       ^~~
a.cc:56:11: error: 'ans' was not declared in this scope; did you mean 'abs'?
   56 |   cout << ans % MOD;
      |           ^~~
      |           abs
a.cc: At global scope:
a.cc:83:1: error: 'll' does not name a type
   83 | ll Factorial(int n){
      | ^~
a.cc:92:1: error: 'll' does not name a type
   92 | ll NPR(int n, int r) {
      | ^~
a.cc:96:1: error: 'll' does not name a type
   96 | ll NCR (int n, int r){
      | ^~