C++ solution O(N*K)
Code

# Code
```cpp []
class Solution {
public:
  unsigned long long mod = 1000000007;
  int minMaxSums(vector<int>& nums, int K) {
    unsigned long long k = K;
    unsigned long long n = nums.size();
    unsigned long long tot = 0;
    vector<vector<unsigned long long>> ncr(nums.size() + 1, vector<unsigned long long> (k + 1,0));
    
    ncr[0][0] = 1;
    for (int i = 1; i <= nums.size(); i++) {
        ncr[i][0] = 1;
        for (int r = 1; r <= k; r++) 
            ncr[i][r] = (ncr[i - 1][r - 1] + ncr[i - 1][r]) % mod;
    } 
    sort(nums.begin(), nums.end());
    for (unsigned long long i = 0; i < n; ++i) {
      unsigned long long s = 1;
      unsigned long long dLeft = i;
      for (unsigned long long r = 0; r <= k - 1; ++r) {
        s += ncr[dLeft][r];
        s %= mod;
      }
      unsigned long long dRight = n - i - 1;
      for (unsigned long long r = 1; r <= k - 1; ++r) {
        s += ncr[dRight][r];
        s %= mod;
      }
      s *= (unsigned long long)nums[i];
      s %= mod;
      tot += s;
      tot %= mod;
    }
    return tot;
  }
};
```
['C++']