JavaScript | With Comments and Explanation [100%, 100%] (Combination + Mathematics)
Approach\nFind the number of ways to sort a list by replacing two elements at a time in given array\n\nPlease upvote me if you like this solution\n\n\n# Code\n\
# Approach\nFind the number of ways to sort a list by replacing two elements at a time in given array\n```\nPlease upvote me if you like this solution\n```\n\n# Code\n```\n// Main Function\nvar numOfWays = function(nums) {\n    return (helper(nums) - 1n) % BigInt(1e9+7) // Return modulo value\n};\n\n// Function return combination (Mathematics) [Formula = n!/( k!*(n-k)!)]\nvar nCr = (n,r) => {\n    if(n < 2) return 1n;\n    n = BigInt(n), r = BigInt(r);\n    return fact(n) / (fact(n-r) * fact(r));\n}\n\nconst cache = new Map(); // Chache for factorials\n// Returns factorial of n [Formula = n!]\nvar fact = (n) => {\n    if(n < 2) return 1n;\n    if(cache.has(n)) return cache.get(n);\n    const res = BigInt(n) * fact(n - 1n);\n    cache.set(n, res);\n    return res;\n}\n\nvar helper = (nums) => {\n    if(nums.length < 3) return 1n;\n\n    const left = [], right = []\n    // Separate lower and higher values than first element in nums\n    for(let i=1; i<nums.length; ++i) {\n        if(nums[i] < nums[0]) left.push(nums[i])\n        if(nums[i] > nums[0]) right.push(nums[i])\n    }\n\n    // Number of possible combinations to get left and right separated\n    const comb = nCr(nums.length-1, left.length)\n    return comb * helper(left) * helper(right);\n}\n```
['JavaScript']