<p>Given an array <code>nums</code> that represents a permutation of integers from <code>1</code> to <code>n</code>. We are going to construct a binary search tree (BST) by inserting the elements of <code>nums</code> in order into an initially empty BST. Find the number of different ways to reorder <code>nums</code> so that the constructed BST is identical to that formed from the original array <code>nums</code>.</p>

<ul>
	<li>For example, given <code>nums = [2,1,3]</code>, we will have 2 as the root, 1 as a left child, and 3 as a right child. The array <code>[2,3,1]</code> also yields the same BST but <code>[3,2,1]</code> yields a different BST.</li>
</ul>

<p>Return <em>the number of ways to reorder</em> <code>nums</code> <em>such that the BST formed is identical to the original BST formed from</em> <code>nums</code>.</p>

<p>Since the answer may be very large, <strong>return it modulo </strong><code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/bb.png" style="width: 121px; height: 101px;" />
<pre>
<strong>Input:</strong> nums = [2,1,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can reorder nums to be [2,3,1] which will yield the same BST. There are no other ways to reorder nums which will yield the same BST.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/ex1.png" style="width: 241px; height: 161px;" />
<pre>
<strong>Input:</strong> nums = [3,4,5,1,2]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The following 5 arrays will yield the same BST: 
[3,1,2,4,5]
[3,1,4,2,5]
[3,1,4,5,2]
[3,4,1,2,5]
[3,4,1,5,2]
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/12/ex4.png" style="width: 121px; height: 161px;" />
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no other orderings of nums that will yield the same BST.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
	<li>All integers in <code>nums</code> are <strong>distinct</strong>.</li>
</ul>

class Solution:
    def numOfWays(self, nums):
        mod = 10**9 + 7
        ans = self.dfs(nums, mod)
        return (ans % mod) - 1

    # Brute-force factorial calculation each time
    def factorial(self, n):
        res = 1
        for i in range(2, n + 1):
            res *= i
        return res

    # Brute-force combination calculation without optimization
    def comb(self, n, k, mod):
        if k > n:
            return 0
        
        # Compute n!, k!, and (n-k)! each time from scratch
        numerator = self.factorial(n)
        denominator = self.factorial(k) * self.factorial(n - k)

        # Use the extended Euclidean algorithm to calculate the inverse
        return (numerator * self.mod_inverse(denominator, mod)) % mod

    # Brute-force modular inverse using the extended Euclidean algorithm
    def mod_inverse(self, a, mod):
        m0, x0, x1 = mod, 0, 1

        if mod == 1:
            return 0

        while a > 1:
            q = a // mod
            t = mod

            # Apply the extended Euclidean algorithm
            mod = a % mod
            a = t
            t = x0
            x0 = x1 - q * x0
            x1 = t

        if x1 < 0:
            x1 += m0

        return x1

    # Recursively calculate the number of ways
    def dfs(self, nums, mod):
        n = len(nums)
        if n <= 2:
            return 1

        # Find left and right subsequences
        left = [num for num in nums[1:] if num < nums[0]]
        right = [num for num in nums[1:] if num >= nums[0]]

        # Recurse for left and right subtrees
        left_res = self.dfs(left, mod) % mod
        right_res = self.dfs(right, mod) % mod

        # Calculate combination on the fly each time
        left_len = len(left)
        combination = self.comb(n - 1, left_len, mod)

        return (((combination * left_res) % mod) * right_res) % mod

21300