Intuition Explained & recursively finding contribution of each element.
IntuitionAs at the end We have to consider only maximum and minimum of each subsequence, order of sequence won't matter.
Therefore we can sort the array.
Now we
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
As at the end We have to consider only maximum and minimum of each subsequence, order of sequence won't matter.
Therefore we can sort the array.
Now we have to figure out How many times will each element, be picked in subsequence and will it contribute as maximum or a minimum.

# Approach
<!-- Describe your approach to solving the problem. -->
As we have already sorted and figured out, that the order doesn't matter.
We can realise that, every element (Let's say ith index element) will be minimum for all the elments ahead of iut, and maximum for all the elements before it.
So all we have to find is the frequency of each element as minimum and maximum. This can be done by recursion considering we have to find count of m length sequence in total of n elements.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    O(n * k)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    O(n * k)

# Code
```cpp []
#define ll long long
#define mod 1000000007

class Solution {
public:

    ll solve(int i, int seqLen, vector < vector < ll > > &dp){ // ways of choosing seqLen amt. of elements from overall i elements

        if(seqLen == 1 || i == 0)   return 1;
        if(dp[i][seqLen] != -1)     return dp[i][seqLen];

        return dp[i][seqLen] = (solve(i - 1, seqLen, dp) + solve(i - 1, seqLen - 1, dp)) % mod;

        /*Instead of directly computing factorials (which can be computationally expensive), we use DP with the following recursive relation:


        C(i, j) = C(i-1, j) + C(i-1, j-1) */

    }

    int minMaxSums(vector<int>& nums, int k) {
        
        ll n = nums.size(), sum = 0;
        sort(nums.begin(), nums.end());

        vector < vector < ll > > dp(n + 1, vector < ll > (k + 1, -1));
        
        for(int i = 0; i < n; i++){

            // Total value of this element after being maximum in all the sequences formed by i elements (0 to i) of lenght(k and less).. 
            //same while considering this element as minimum in all the sequences of size (k & less) starting from i to n - 1th index

            ll maxi = (nums[i] * solve(i, k, dp)) % mod;
            ll mini = (nums[i] * solve(n - i - 1, k, dp)) % mod;

            sum = (sum + maxi + mini) % mod;
            // it might look like i am calling solve for each element making TC (n * k) ^ 2, but its not, cause of DP, all the possibilites will be precalculated and will be return by if(dp[i][seqLen] != -1)     return dp[i][seqLen]; in O(1).
        }

        return sum;
        
    }
};
```
['Dynamic Programming', 'Recursion', 'Combinatorics', 'C++']