[c++]  factorial and inv factorial (no dp)
IntuitionApproachComplexity
Time complexity:
O(n * k)
Space complexity:
O(n)Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# 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)
# Code
```cpp []
typedef long long ll;
class Solution {
public:
    const ll mod = (ll)(1e9 + 7);
    ll pow2(ll i)
    {
        ll base = 2;
        ll bit = 1;
        ll ans = 1;
        while (bit <= i)
        {
            if ((bit & i) == bit)
            {
                ans = ans * base % mod;
            }
            base = base * base % mod;
            bit = bit << 1;
        }
        return ans;
    }
    vector<ll> factor;
    vector<ll> invfactor;

    ll pow(ll x, ll c)
    
    {
        ll base = x;
        ll bit = 1;
        ll ans = 1;
        while (bit <= c)
        {
            if ((bit & c) == bit)
            {
                ans = (ans * base) % mod;
            }
            base = (base * base) % mod;
            bit = bit << 1;
        }
        return ans;
    }

    ll inv(ll x)
    {
        ll ans = pow(x, mod - 2);
        return ans;
    }
    ll C(ll n, ll k)
    {
        if (n == k) return 1;
        else if (k == 0) return 1;
        if (n < k) return 0; 
        return factor[n] * invfactor[n - k] % mod * invfactor[k]  % mod;
    }

    ll sumc(ll n, ll k)
    {
        ll mx = min(n, k);
        ll ans = 0;
        for (ll i = 0; i <= mx; i++)
        {
            ll cur = C(n, i);
            //cout << i << "," << cur << endl;
            ans = (ans + cur) % mod;
        }
        return ans;
    }
    int minMaxSums(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        ll ans = 0;
        factor = vector<ll>(n + 1, 1);
        for (int i = 1; i <= n; i++)
        {
            factor[i] = factor[i - 1] * i % mod; 
        }
        invfactor = vector<ll>(n + 1, 1);
        for (int i = 0; i <= n; i++)
        {
            invfactor[i] = inv(factor[i]);
        }
        for (int i = 0; i < n; i++)
        {
            int x = i;
            int y = n - x - 1;
            ll v = nums[i];
            ll a = sumc(x, k - 1);
            ll b = sumc(y, k - 1);
            //cout  << i << "," << a << "," << b << "," << x << "," << y << endl;
            ans = (ans + (a * v % mod)) % mod;
            ans = (ans + (b * v % mod)) % mod; 
        }
        return ans;
    }
};
```
['C++']