Combinatronics || O(N*k) 
Intuitioninstead of finding all the subsequences and finding the min and max within themtry the other way around , lets see in how different subsequences this n
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
instead of finding all the subsequences and finding the min and max within them

try the other way around , lets see in how different subsequences this number is minimum and maximum 

ex: if there are 5 elements that are <= this number 'X'

then taking this number and pairing them with any other of this 5 elements also , 'X' will be the maximum element 

notice that subsequence doesnt matter anymore

we are only bothered about the number of elements greater or lesser than this number 

hence we can sort the array and continue solving





# Approach
<!-- Describe your approach to solving the problem. -->
use of fermats theorem to calculate the ncr

among 'y' elements you can pick 1, 2, 3.., but not more than k-1 because you are already considering this element 

calculate all the ways, and add the contribution of the number to the answer 


# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    O(1e5+ NlogN + N*k) ~= O(N*k)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    O(N + N ) ~= O(N)

# Code
```cpp []
#define ll long long
ll mod = 1e9 + 7;
const ll N = 2e5 + 1;

ll fact[N], invfact[N];

ll powm(ll a, ll b)
{
    if (b == 1)
        return a;
    ll res = 1ll;
    if (b % 2 == 0)
    {
        res = powm(a, b / 2);
        res = (res * res) % mod;
    }
    else
    {
        res = powm(a, b - 1);
        res = (res * a) % mod;
    }

    return res;
}

void init()
{
    fact[0] = 1;
    ll i = 1;
    for (i; i < N; i++)
    {
        fact[i] = (fact[i - 1] * i) % mod;
    }

    i = N-1;
    invfact[i] = powm(fact[i], mod - 2);
    i--;
    for (i; i >= 0; i--)
    {
        invfact[i] = ((i + 1ll) * invfact[i + 1]) % mod;
    }

    return;
}

ll ncr(ll n, ll r){

    if(r > n || n<0 || r < 0) return 0;

    ll ans = fact[n];
    ans = (ans * invfact[n-r])%mod;
    ans = (ans * invfact[r])%mod;


    return ans;

}



class Solution {
public:

    Solution(){
        init();
    }
    

    ll solve(vector<int>&nums, ll n, ll k){
        
        ll ans = 0;
        sort(nums.begin(), nums.end());
        for(ll i = 0; i<n; i++){

            ll remlen = k-1;  //max in all these subsequences
            ll smaller_elements = i;
            ll ways = 0; //choosing single element
            for(ll j = 1; j<=min(remlen, smaller_elements); j++){
                ll tmp = ncr(smaller_elements, j);

                ways = (ways + tmp)%mod;
            }

            //same thing for mini 
            ll larger_elements = n-1-i;
            remlen = k-1;
            for(ll j = 1; j<=min(remlen, larger_elements); j++){
                ll tmp = ncr(larger_elements, j);
                ways = (ways + tmp)%mod;
            }

            ways += 2;

            ways = (ways*(1ll*nums[i]))%mod;


            ans = (ans + ways)%mod;

        }

        return ans;
        
    }
    
    
    int minMaxSums(vector<int>& nums, int k) {
        //init at constructor call 
        ll n = nums.size();
        ll ans = solve(nums, n, k);
        return ans;
    }
};
```
['C++']