Simple combination logic
IntuitionFor each index if i am able to find that in how many subsequences the element at index i is smallest and those in which it will be largest then i can f
# Intuition
For each index if i am able to find that in how many subsequences the element at index i is smallest and those in which it will be largest then i can find the total answer as well.Here we can change the order of elements as we only minimum and maximum.

# Approach
### Explanation of the Approach

1. **Sorting the Array**  
   The array is sorted to facilitate computation of "minimum" and "maximum" contributions.

2. **Base Case (\( k = 1 \))**:  
   If \( k = 1 \), the result is simply the sum of the array values multiplied by 2 (as each element contributes to both minimum and maximum sums).

3. **Combination Contributions**:  
   For \( k > 1 \), the contribution of each array element to the result is calculated using combinatorics:

   - **Left Contribution (Minimums)**:  
     For element at index \( i \), compute how many ways it can form a group of size \( j \) with smaller elements, given \( j - 1 \) other elements are chosen from the first \( i \) elements.  
     Contribution formula:  
     {Left Contribution} = {array[i]} * C(i, j-1)

   - **Right Contribution (Maximums)**:  
     Similarly, compute how many ways it can form a group of size \( j \) with larger elements, given \( j - 1 \) other elements are chosen from the \( n - i - 1 \) elements to the right.  
     Contribution formula:  
     {Right Contribution} = {array[i]} * C({size\_} - i - 1, j-1)

    You can calculate combinations by the aove snippet used.

4. **Summing Contributions**:  
   For each element and for \( j \) from 2 to \( k \), both contributions (left and right) are added to the result modulo  {MOD} \.

# Complexity
- Time complexity:
- O(size_⋅(log(size_)+k))

- Space complexity:
O(size_)

# Code
```cpp []
class Solution {
public:
    const long long MOD = 1e9+7;
    long long* f;
    long long* if_;
void solve1(int size_) {
        f = new long long[size_ + 1];
        if_ = new long long[size_ + 1];
        
        f[0] = 1;
        for (int i = 1; i <= size_; i++) {
            f[i] = (f[i - 1] * i) % MOD;
        }
        
        if_[size_] = exponentiation(f[size_], MOD - 2, MOD);
        for (int i = size_ - 1; i >= 0; i--) {
            if_[i] = (if_[i + 1] * (i + 1)) % MOD;
        }
    }
    

    

    long long find_(int size_, int r) {
        if (r < 0 || r > size_) return 0;
        return ((f[size_] * if_[r]) % MOD * if_[size_ - r]) % MOD;
    }
    long long exponentiation(long long b, long long exp, long long MOD) {
        long long ans = 1;
        while (exp > 0) {
            if (exp % 2 == 1) ans = (ans * b) % MOD;
            b = (b * b) % MOD;
            exp /= 2;
        }
        return ans;
    }

    int minMaxSums(vector<int>& array, int k) {
        int size_ = array.size();
        solve1(size_);
        long long ans = 0;
        sort(array.begin(), array.end());

        for (auto num : array) {
            ans = (ans + num * 2LL) % MOD;
        }

        if (k == 1) {
            return ans;
        }

        for (int i = 0; i < size_; ++i) {
            for (int j = 2; j <= k; ++j) {
                 ans = (ans + array[i] * find_(size_ - i - 1, j - 1)) % MOD;
                ans = (ans + array[i] * find_(i, j - 1)) % MOD;
               
            }
        }
        return ans;
    }
};

```
['C++']