maths, commented
we sort the array since choosing subsequence from the original array and the same elements in the sorted array wouldnt play a difference in the answer(min and m
we sort the array since choosing subsequence from the original array and the same elements in the sorted array wouldnt play a difference in the answer(min and max element is same)
# Code
```cpp []

const int MOD = 1e9 + 7;

class Solution {
public:
    //  to compute power (x^y % mod)
    long long modPow(long long x, long long y, int mod) {
        long long res = 1;
        while (y > 0) 
        {
            if (y % 2) res = res * x % mod; //odd power
            x = x * x % mod;
            y >>= 1;
        }
        return res;
    }

    // compute factorials and inverse factorials
    void getfactorials(vector<long long>& fact, vector<long long>& invFact, int n) 
    {
        fact[0] = invFact[0] = 1;
        for (int i = 1; i <= n; i++) {
            fact[i] = fact[i - 1] * i % MOD;
        }
        invFact[n] = modPow(fact[n], MOD - 2, MOD); //fermats little theorem
        for (int i = n - 1; i >= 1; i--) {
            invFact[i] = invFact[i + 1] * (i + 1) % MOD;
        }
    }

    // function to calculate ncr % mod
    long long ncr(int n, int r, vector<long long>& fact, vector<long long>& invFact) 
    {
        if (r > n || r < 0) 
            return 0;
        return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD;
    }

    int minMaxSums(vector<int>& nums, int k) 
    {
        int n = nums.size();
        sort(nums.begin(), nums.end()); // sort the array

        vector<long long> fact(n + 1), invFact(n + 1);
        getfactorials(fact, invFact, n);

        long long result = 0;

        for (int i = 0; i < n; i++) 
        {
            for (int size = 1; size <= k; size++) 
            {
                // contribution as minimum -> we assume current element nums[i] is the minimum of 
                // some subsequence, so we find number of times it is a minimum for the current size.
                // remaining elements(greater) = n - i - 1 ; elements required to fill for current size = size - 1
                long long minContribution = nums[i] * ncr(n - i - 1, size - 1, fact, invFact) % MOD;
                // contribution as maximum(same like minimum)
                long long maxContribution = nums[i] * ncr(i, size - 1, fact, invFact) % MOD;

                result = (result + maxContribution + minContribution) % MOD;
            }
        }

        return (int)result;
    }
};

```
['C++']