You are given an integer array nums and a positive integer k. Return the sum of the maximum and minimum elements of all subsequences of nums with at mostk elements.
Since the answer may be very large, return it modulo109 + 7.
longlong mod = 1e9 + 7; structCombination { vector<longlong> fac, inv; longlong n, MOD;
longlongmodpow(longlong n, longlong x, longlong MOD = mod){ if(!x) return1; longlong res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }
Combination(longlong _n, longlong MOD = mod): n(_n + 1), MOD(MOD) { inv = fac = vector<longlong>(n,1); for(int i = 1; i < n; i++) fac[i] = fac[i-1] * i % MOD; inv[n - 1] = modpow(fac[n - 1], MOD - 2, MOD); for(int i = n - 2; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % MOD; }
longlongfact(longlong n){return fac[n];} longlongnCr(longlong n, longlong r){ if(n < r or n < 0or r < 0) return0; return fac[n] * inv[r] % MOD * inv[n-r] % MOD; } };