Easy C++ Solution with Explanation || O(n.k) || Mathematic Combinatorics
ApproachSubsequences Containing nums[i] as the Minimum Element:
For each element nums[i], consider it as the smallest element in a subsequence of size j (rangin
# Approach

#### Subsequences Containing nums[i] as the Minimum Element:

- For each element nums[i], consider it as the smallest element in a subsequence of size j (ranging from 1 to k).
- The number of ways to choose the remaining j-1 elements from the first i elements is given by comb(i, j-1).
- The contribution of nums[i] as the minimum element to the subsequence sum is nums[i] * comb(i, j-1), and this is added to the cnt variable.

#### Subsequences Containing nums[i] as the Maximum Element:

- Similarly, consider nums[i] as the largest element in a subsequence of size j.
- The number of ways to choose the remaining j-1 elements from the elements after nums[i] is given by comb(n-i-1, j-1), where n is the size of the array.
- The contribution of nums[i] as the maximum element to the subsequence sum is nums[i] * comb(n-i-1, j-1), and this is added to the cnt variable.

#### Final Answer: The total count cnt is computed modulo mod and returned as the result.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(nlog + n.k)$$

- Space complexity:
$$O(100000)$$

# Code
```cpp []
class Solution {
public:
    typedef long long ll;
    int mod = 1e9 + 7;
    vector<ll> f, factInv;

    Solution() {
        f.resize(100000);
        factInv.resize(100000);
        f[0] = 1;
        for(int i = 1; i < 100000; i++){
            f[i] = f[i-1] * i % mod;
        }
        
        factInv[100000 - 1] = binpow(f[100000 - 1], mod - 2);
        for(int i = 100000 - 2; i >= 0; i--){
            factInv[i] = factInv[i+1] * (i+1) % mod;
        }
    }

    ll binpow(ll a, ll b){
        ll res = 1;
        while (b) {
            if (b % 2) {
                res = (res * a) % mod;
            }
            a = (a * a) % mod;
            b /= 2;
        }
        return res;
    }

    ll comb(ll n, ll r) {
        if (r > n || r < 0) return 0;
        return f[n] * factInv[r] % mod * factInv[n - r] % mod;
    }

    int minMaxSums(vector<int>& nums, int k) {
        ll n = nums.size();
        sort(nums.begin(), nums.end());
        ll cnt = 0;
        for(ll i = 0; i < n; i++){
            for(ll j = 1; j <= k; j++){
                if (i >= j - 1) {
                    ll mini = (nums[i] * comb(i, j - 1)) % mod;
                    cnt = (cnt + mini) % mod;
                }
            }
            for(ll j = 1; j <= k; j++){
                if(n - i - 1 >= j - 1){
                    ll maxi = (nums[i] * comb(n - i- 1, j - 1)) % mod;
                    cnt = (cnt + maxi) % mod;
                }
            }
        }
        return cnt%mod;
    }
};

```
['Math', 'Combinatorics', 'C++']