Easy precomputation approach
Intuitionusing permutaion approachApproachfermat little theorem to calculate modComplexity
Time complexity:
o(n*k),precomputaion of factorial
Code
# Intuition
using permutaion approach
# Approach
fermat little theorem to calculate mod
# Complexity
- Time complexity:
o(n*k),precomputaion of factorial

# Code
```cpp []
#define ll long long int
const int mod = 1e9 + 7;

// Function to calculate modular exponentiation
ll modExp(ll base, ll exp, ll mod) {
    ll result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) result = (result * base) % mod;
        base = (base * base) % mod;
        exp /= 2;
    }
    return result;
}

// Function to calculate modular inverse using modular exponentiation
ll modInverse(ll num, ll mod) {
    return modExp(num, mod - 2, mod);
}

// Precompute factorials and modular inverses
void precomputeFactorials(vector<ll>& fact, vector<ll>& invFact, int maxN) {
    fact[0] = invFact[0] = 1;
    for (int i = 1; i <= maxN; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
        invFact[i] = modInverse(fact[i], mod);
    }
}

// Function to calculate nCr % mod
ll nCr(int n, int r, const vector<ll>& fact, const vector<ll>& invFact) {
    if (r > n) return 0;
    return (fact[n] * invFact[r] % mod * invFact[n - r] % mod) % mod;
}

class Solution {
public:
    int minMaxSums(vector<int>& nums, int k) {
        int n = nums.size();
        sort(nums.begin(), nums.end());

        vector<ll> fact(n + 1), invFact(n + 1);
        precomputeFactorials(fact, invFact, n);

        ll ans = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= k - 1 && j <= n - i - 1; j++) {
                ll ncr = nCr(n - i - 1, j, fact, invFact);
                ll sumPair = (nums[i] + nums[n - i - 1]) % mod;
                ans = (ans + ncr * sumPair % mod) % mod;
            }
        }

        return ans;
    }
};

```
['C++']