O(NlogN) count how many times a number appear as max & min
IntuitionQuite a tricky question. Originally I tried to count how many sequences are there for each pair of (min, max), then it got TLE because it's an O(N^2) a
# Intuition
Quite a tricky question. Originally I tried to count how many sequences are there for each pair of (min, max), then it got TLE because it's an O(N^2) algorithm. Then I realized you just need to calculate how many times a number appear as the minimum or maximum.

# Approach
You will get a TLE if you use Python's `math.comb`. And you will get a TLE too if you just cash the results when you call `math.comb`. So we got no choice but to use DP to cache results of `math.comb` in a dictionary. This is actually the harder part because you have to set `comb[i][0] = 1` otherwise you will never get the comb DP right.

# Complexity
- Time complexity:
O(max(N * K, NlogN))

- Space complexity:
O(N * K)

# Code
```python3 []
class Solution:
    def minMaxSums(self, nums: List[int], k: int) -> int:
        nums.sort()
        mod = 10 ** 9 + 7
        result = 0

        comb = [[0 for _ in range(1 + k)] for _ in range(len(nums) + 1)]
        comb[0][0] = 1
        for i in range(1, len(nums) + 1):
            comb[i][0] = 1
            for j in range(1, 1 + k):
                comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]

        for i in range(len(nums)):
            for x in range(1, 1 + k):
                result = (result + nums[i] * (comb[i][x - 1] + comb[len(nums) - 1 - i][x - 1])) % mod
        return result

```
['Python3']