JavaScript Solution | Recursion + Combination
\nvar numOfWays = function(nums) {\n    const x = numOfWaysHelper(nums) - 1n;\n    return x % 1_000_000_007n;\n}\nvar numOfWaysHelper = function(nums) {\n    if
```\nvar numOfWays = function(nums) {\n    const x = numOfWaysHelper(nums) - 1n;\n    return x % 1_000_000_007n;\n}\nvar numOfWaysHelper = function(nums) {\n    if(nums.length < 3)\n        return 1n;\n    \n    const root  = nums[0];\n    const left  = nums.filter(p => p < root);\n    const right = nums.filter(p => p > root);\n    return BigInt(comb(left.length + right.length, left.length) * numOfWaysHelper(left) * numOfWaysHelper(right));\n};\n\nfunction comb(n, k){\n    n = BigInt(n);\n    k = BigInt(k);\n    if(n < 2)\n        return 1n;\n    return fact(n)/fact(n-k)/fact(k);\n}\n\nconst factCache = new Map();\n\nfunction fact(n){\n    if(n<2)\n        return 1n;\n    if(factCache.has(n))\n        return factCache.get(n);\n    \n    var result = BigInt(n) * fact(n - 1n);\n    factCache.set(n, result);\n    return result;\n}\n```
['JavaScript']