Binomial coefficient, Pascal's triangle
Code
# Code
```csharp []
public class Solution {
    const int M = 1000000007;

    int[,] _bks;
  
    public int MinMaxSums(int[] nums, int k) {
        int n = nums.Length;
        
        CalculateBk(n, k);

        Array.Sort(nums);

        long sum = new();
        
        for (int i = 0; i < n; i++) {
            long count = 2; // when only this number is selected then it's both min and max
            // subsequences of other lengths, where length is l + 1, l is number of numbers we are missing to form this length
            for (int l = 1; l < k; l++) {
                // number of times nums[i] can be used as a min
                // n is number of numbers greater than nums[i]
                count += GetBk(n - i - 1, l);
                // number of times it can be used as a max
                // n is number of numbers smaller than nums[i]
                count += GetBk(i, l);
                count %= M;
            }

            sum += nums[i] * count;
            sum %= M;
        }

        return (int)sum;
    }

    // Pascal's triangle
    void CalculateBk(int n, int k) {
        _bks = new int[n + 1, k];
        _bks[0, 0] = 1;

        for (int n1 = 1; n1 <= n; n1++) {
            _bks[n1, 0] = 1;
            for (int k1 = 1; k1 < k; k1++) {
                _bks[n1, k1] = (_bks[n1 - 1, k1] + _bks[n1 - 1, k1 - 1]) % M;
            }
        }
    }

    int GetBk(int n, int k) => k > n ? 0 : _bks[n, k];
}
```
['C#']