Using nCr with fermats little theoram
IntuitionSort the array. now see all items how many times each item become minimum & maximum in subsequence for all possible subsets of size r.
consider ex:if y
# Intuition
Sort the array. now see all items how many times each item become minimum & maximum in subsequence for all possible subsets of size r.
consider ex:
```
nums[] = [1,2,3,4,5], k = 5

conside 1, see how many times it becomes minimum for r=1 to k
    r = 1, number of times it is min -> 1. (1)
    r = 2, number of times it is min -> 4. ((1,2),...(1,5))
    r = 3, number of times it is min -> 6. ((1,2,3),...(1,4,5))
    r = 4, number of times it is min -> 4. ((1,2,3,4),...(1,3,4,5))
    r = 5, number of times it is min -> 1. (1,2,3,4,5)
conside 2, see how many times it becomes minimum for r=1 to k
    r = 1, number of times it is min -> 1. (2)
    r = 2, number of times it is min -> 3. ((2,3),...(2,5))
    r = 3, number of times it is min -> 3. ((2,3,4),...(2,4,5))
    r = 4, number of times it is min -> 1. ((2,3,4,5))
    r = 5, number of times it is min -> 0. ()
consider 3,
    same way, it is [1,2,1,0,0]
consider 4,
    it is [1,1,0,0,0]
consider 5,
     it is [1,0,0,0,0]
```
if you sea the numbers it is nCr combinations.
so, you can iterate from n-1 to 1 find the value and multiple with nums[i] and add to result.

to find nCr it is most common technique. you can read about **fermats little theoram**. it says,
If p is a prime number and a is an integer, then:
    a^p  ≡ a  (mod p)
ex: a=2, p=5
    LHS 2^5 (mod 5) = 32 (mod 5) = 2
    RHS 2



# Complexity
- Time complexity:
O(n*k)

- Space complexity:
O(n*k)

# Code
```java []
class Solution {
    static final int mod = 1000000007;
	static int[][] fif = enumFIF(100005, mod);
    public static long nCr(int n, int r, int mod, int[][] fif) {
		if (n < 0 || r < 0 || r > n) return 0;
			return (long) fif[0][n] * fif[1][r] % mod * fif[1][n - r] % mod;
	}

	public static int[][] enumFIF(int n, int mod) {
		int[] f = new int[n + 1];
		int[] invf = new int[n + 1];
		f[0] = 1;
		for (int i = 1; i <= n; i++) {
			f[i] = (int) ((long) f[i - 1] * i % mod);
		}
		long a = f[n];
		long b = mod;
		long p = 1, q = 0;
		while (b > 0) {
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		invf[n] = (int) (p < 0 ? p + mod : p);
		for (int i = n - 1; i >= 0; i--) {
			invf[i] = (int) ((long) invf[i + 1] * (i + 1) % mod);
		}
		return new int[][]{f, invf};
    }	

	static long pow(long base, long exp) {
        long result = 1;
        while (exp > 0) {
            if (exp % 2 == 1) {
                result = (result * base) % mod;
            }
            base = (base * base) % mod;
            exp /= 2;
        }
        return result;
    }
    public int minMaxSums(int[] nums, int k) {
        long res = 0;
        int n = nums.length;
        Arrays.sort(nums);
        int len = n-1;
        for(int i=0;i<n;i++){
            for(int j=0;j<=Math.min(k-1,len);j++){
                res = (res + (nums[i] * nCr(len, j, mod, fif)) % mod) % mod;
            }
            len--;
        }
        len = n-1;
        for(int i=n-1;i>=0;i--){
            for(int j=0;j<=Math.min(k-1,len);j++){
                res = (res + (nums[i] * nCr(len, j, mod, fif)) % mod) % mod;
            }
            len--;
        }
        
        return (int)res % mod;
    }
}
```
['Math', 'Sorting', 'Combinatorics', 'Counting', 'Java']