Python Beat 100%! Hope you can understand the problem better!
IntuitionFor each element in nums, we calculate how many subsequences that element appears in.Approach

After rearranging the nums array, for example, we take t
# Intuition
![Screenshot 2025-01-19 175316.png](https://assets.leetcode.com/users/images/1bcdd6ea-55a9-41c9-ad72-50ad3491eff6_1737284024.9154305.png)
For each element in nums, we calculate how many subsequences that element appears in.
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach

- After rearranging the nums array, for example, we take the first element, called x (the smallest element): call n = len(nums)
- With a subsequence with 1 element -> x only appears in 1 subsequence, which is [x] -> 1 sequence
- With a subsequence with 2 elements -> choose 1 element in the remaining n - 1 elements of the sequence -> there are n - 1 sequences
- Continuing to think in this way, with the remaining elements we will get a Pascal's triangle.
- From there, we can see that the sum of all the smallest elements of all the subsequences will be the sum of the products between the sums of the rows in the Pascal's triangle and the elements in the corresponding array.
- Similarly with the largest element, we deduce the Problem Result.

- Note that: Representing the entire Pascal's triangle is definitely not possible for n < 10^5, so we will only need 1 array to store the previous state of each row in the pascal's triangle to calculate the current state.

# Complexity
- Time complexity: O(nlogn)
# Code
```python3 []
class Solution:
    def minMaxSums(self, nums: List[int], k: int) -> int:
        n = len(nums)
        length = max(k, n)
        nums.sort()
        arr = [0] * length
        arr[0] = 1
        res = 0
        mod = 10**9 + 7
        for i in range(n):
            x = nums[i]
            y = nums[n - i - 1]
            # Iterate backwards from k to 1 to avoid overwriting the state
            for j in range(min(k, i), 0, -1):
                arr[j] += arr[j - 1]
            res += (sum(arr[:k]) % mod * ((x + y) % mod)) % mod
            # print(arr, sum(arr[:k]) * (x + y), x, y)
        return res % mod


```
Thanks for watching, if there is a better way or there is an unclear explanation, please let me know!
['Python3']