slow, but works
We count the numbers strictly smaller than x, equal to x and strictly larger. They are a[x], m[x], b[x]. From those we can count how often is x min and max.
We count the numbers strictly smaller than x, equal to x and strictly larger. They are a[x], m[x], b[x]. From those we can count how often is x min and max.

```python3 []
from functools import cache
from math import comb

@cache
def cdf(n,k):
    return sum(comb(n,i) for i in range(k+1)) % 1_000_000_007

@cache
def f(k, a, b, m):
    return sum(comb(m,l) * (cdf(a,k-l) + cdf(b, k-l)) for l in range(1, 1+min(k, m))) % 1_000_000_007


class Solution:
    def minMaxSums(self, nums: List[int], k: int) -> int:
        nums.sort()
        a = {}
        b = {}  
        m = {} 
        n = len(nums) 
        y = nums[0]
        b[nums[-1]] = 0
        m[nums[0]] = 0
        a[nums[0]] = 0
        for i, x in enumerate(nums):
            if y < x:
                a[x] = i
                m[x] = 1
                b[y] = n-i
                y = x
            else:
                m[x] += 1

        return sum(x * f(k, a[x], b[x], m[x]) for x in set(nums)) % 1_000_000_007

```
['Python3']