C++ || Explained
\n\n# Approach\n The solution first calculates the binomial coefficients using dynamic programming and stores them in a 2D vector comb. \nThen, it defines a rec
\n\n# Approach\n The solution first calculates the binomial coefficients using dynamic programming and stores them in a 2D vector comb. \nThen, it defines a recursive function dfs that takes as input a subarray of nums and returns the number of ways to reorder that subarray such that the BST formed is identical to the original BST.\n The function dfs first checks if the size of the input subarray is less than or equal to 2, in which case it returns 1.\n Then, it separates the elements of the input subarray into two subarrays left and right, where left contains all elements that are less than the first element of the input subarray and right contains all elements that are greater than or equal to the first element of the input subarray. \nThe function then calculates the result as the product of the binomial coefficient comb[n - 1][left.size()], dfs(left), and dfs(right), modulo 1000000007. Finally, it returns the result cast to an integer. The main function calls dfs(nums) and returns its result minus 1\n\n# Complexity\n- Time complexity:O(n^2)\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n        int numOfWays(vector<int>& nums) {\n        int n = nums.size();\n        vector<vector<int>> comb(n + 1, vector<int>(n + 1));\n        comb[0][0] = 1;\n        for (int i = 1; i <= n; ++i) {\n            comb[i][0] = 1;\n            for (int j = 1; j <= i; ++j) {\n                comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % 1000000007;\n            }\n        }\n        function<int(vector<int>&)> dfs = [&](vector<int>& nums) {\n            int n = nums.size();\n            if (n <= 2) return 1;\n            vector<int> left, right;\n            for (int i = 1; i < n; ++i) {\n                if (nums[i] < nums[0]) left.push_back(nums[i]);\n                else right.push_back(nums[i]);\n            }\n            long long res = comb[n - 1][left.size()];\n            res = res * dfs(left) % 1000000007;\n            res = res * dfs(right) % 1000000007;\n            return (int)res;\n        };\n        return dfs(nums) - 1;\n    }\n};\n   \n```
['Dynamic Programming', 'Combinatorics', 'C++']