2
var numOfWays = function (nums) {3
return (helper(nums) - 1n) % BigInt(1e9 + 7); // Return modulo value6
// Function return combination (Mathematics) [Formula = n!/( k!*(n-k)!)]9
(n = BigInt(n)), (r = BigInt(r));10
return fact(n) / (fact(n - r) * fact(r));13
const cache = new Map(); // Chache for factorials14
// Returns factorial of n [Formula = n!]17
if (cache.has(n)) return cache.get(n);18
const res = BigInt(n) * fact(n - 1n);23
var helper = (nums) => {24
if (nums.length < 3) return 1n;28
// Separate lower and higher values than first element in nums29
for (let i = 1; i < nums.length; ++i) {30
if (nums[i] < nums[0]) left.push(nums[i]);31
if (nums[i] > nums[0]) right.push(nums[i]);34
// Number of possible combinations to get left and right separated35
const comb = nCr(nums.length - 1, left.length);36
return comb * helper(left) * helper(right);