Language: C++

// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define msi map<string,int>
#define mii map<int,int>
#define viip vector<pair<int,pair<int,int> > > 
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back 
#define po pop_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	#ifndef ONLINE_JUDGE
    	freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
	#endif
	ll n,k;
	cin>>n>>k;
	vi v(n);
	rep(i,n){
		cin>>v[i];
	}	
	ll max1 = 0;
	ll x  = 1;
	repa(i,1,n){
		x *= i;
		x %= mod;
	}
	ll y = 1;
	repa(i,1,k){
		y *= i;
		y %= mod;
	}
	ll z = 1;
	repa(i,1,n-k+1){
		z *= i;
		z %= mod;
	}
	x *= inverse(y);
	x %= mod;
	x *= inverse(z);
	x %= mod;
	ll x1 = x;
	sort(all(v),greater<ll>());
	rep(i,n-k+1){	
		ll temp = v[i];
		temp *= x;
		temp %= mod;
		max1 += temp;
		max1 %= mod;
		x *= inverse(n-i-1);
		x %= mod;
		x *= (n-k-i);
		x %= mod;
	}
	sort(all(v));
	x = x1;
	ll min1 = 0;
	rep(i,n-k+1){	
		ll temp = v[i];
		temp *= x;
		temp %= mod;
		min1 += temp;
		min1 %= mod;
		x *= inverse(n-i-1);
		x %= mod;
		x *= (n-k-i);
		x %= mod;
	}
	ll ans = max1 - min1;
	ans += mod;
	ans %= mod;
	cout<<ans;
	return 0;
}// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define msi map<string,int>
#define mii map<int,int>
#define viip vector<pair<int,pair<int,int> > > 
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back 
#define po pop_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	ll n,k;
	cin>>n>>k;
	vi v(n);
	rep(i,n){
		cin>>v[i];
	}	
	ll max1 = 0;
	ll x  = 1;
	repa(i,1,n){
		x *= i;
		x %= mod;
	}
	ll y = 1;
	repa(i,1,k){
		y *= i;
		y %= mod;
	}
	ll z = 1;
	repa(i,1,n-k+1){
		z *= i;
		z %= mod;
	}
	x *= inverse(y);
	x %= mod;
	x *= inverse(z);
	x %= mod;
	ll x1 = x;
	sort(all(v),greater<ll>());
	rep(i,n-k+1){	
		ll temp = v[i];
		temp *= x;
		temp %= mod;
		max1 += temp;
		max1 %= mod;
		x *= inverse(n-i-1);
		x %= mod;
		x *= (n-k-i);
		x %= mod;
	}
	sort(all(v));
	x = x1;
	ll min1 = 0;
	rep(i,n-k+1){	
		ll temp = v[i];
		temp *= x;
		temp %= mod;
		min1 += temp;
		min1 %= mod;
		x *= inverse(n-i-1);
		x %= mod;
		x *= (n-k-i);
		x %= mod;
	}
	ll ans = max1 - min1;
	ans += mod;
	ans %= mod;
	cout<<ans;
	return 0;
}

Compiler output:
a.cc:137:4: error: redefinition of 'long long int mod'
  137 | ll mod = 1000000007;
      |    ^~~
a.cc:26:4: note: 'long long int mod' previously defined here
   26 | ll mod = 1000000007;
      |    ^~~
a.cc:138:9: error: redefinition of 'std::mt19937 rng'
  138 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
      |         ^~~
a.cc:27:9: note: 'std::mt19937 rng' previously declared here
   27 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
      |         ^~~
a.cc:139:4: error: redefinition of 'long long int gcd(long long int, long long int)'
  139 | ll gcd(ll a,ll b){
      |    ^~~
a.cc:28:4: note: 'long long int gcd(long long int, long long int)' previously defined here
   28 | ll gcd(ll a,ll b){
      |    ^~~
a.cc:143:4: error: redefinition of 'long long int fastexp(long long int, long long int)'
  143 | ll fastexp(ll x,ll a){
      |    ^~~~~~~
a.cc:32:4: note: 'long long int fastexp(long long int, long long int)' previously defined here
   32 | ll fastexp(ll x,ll a){
      |    ^~~~~~~
a.cc:154:4: error: redefinition of 'long long int inverse(long long int)'
  154 | ll inverse(ll n){
      |    ^~~~~~~
a.cc:43:4: note: 'long long int inverse(long long int)' previously defined here
   43 | ll inverse(ll n){
      |    ^~~~~~~
a.cc:157:5: error: redefinition of 'int main()'
  157 | int main() {
      |     ^~~~
a.cc:46:5: note: 'int main()' previously defined here
   46 | int main() {
      |     ^~~~