[C++] Well commented and well explained
Calculate appearnces of each element in Min and Max columns.Code
Calculate appearnces of each element in Min and Max columns.

# Code
```cpp []
class Solution {
public:
    long long MOD = 1000000007;
    vector<long long> fac, invFac; // for factorials and inverse factorials
    // Fast exponentiation
    long long modExp(long long base, int exp) 
    {
        long long res = 1;
        while(exp) {
            if(exp & 1) res = (res * base) % MOD;
            base = (base * base) % MOD; 
            exp >>= 1;
        }
        return res;
    }

    // Precompute factorials and inverses up to n
    void buildFactorials(int n) 
    {
        fac.resize(n+1,1); invFac.resize(n+1,1);
        for(int i=1;i<=n;i++) fac[i] = fac[i-1]*i % MOD;
        invFac[n] = modExp(fac[n], MOD-2); // Fermat's little theorem
        for(int i=n-1;i>=0;i--) invFac[i] = invFac[i+1]*(i+1)%MOD;
    }

    // nCr with precomputed factorials
    long long nCr(int n,int r)
    {
        if(r<0||r>n) return 0;
        return fac[n]*((invFac[r]*invFac[n-r])%MOD)%MOD;
    }

    int minMaxSums(vector<int>& nums, int k) 
    {
        sort(nums.begin(), nums.end());
        int n = (int)nums.size();
        buildFactorials(n); // precompute for up to size n

        long long ans=0;
        for(int i=0;i<n;i++){
            // timesAsMin = sum_{l=1..k} C(n-i-1, l-1)
            long long timesAsMin=0, timesAsMax=0;
            for(int l=1;l<=k;l++){
                timesAsMin=(timesAsMin+nCr(n-i-1,l-1))%MOD;
                timesAsMax=(timesAsMax+nCr(i,l-1))%MOD;
            }
            long long total=(timesAsMin+timesAsMax)%MOD;
            ans=(ans+ total*(nums[i]%MOD)%MOD)%MOD;
        }
        return (int)ans;
    }
};
```
['C++']