[Python 3] Solution with real BST
Code\n\nfrom math import comb\n\nMOD = 10**9+7\n\nclass Solution:\n    def numOfWays(self, nums: List[int]) -> int:\n        root = self._build_bst(nums)\n     
# Code\n```\nfrom math import comb\n\nMOD = 10**9+7\n\nclass Solution:\n    def numOfWays(self, nums: List[int]) -> int:\n        root = self._build_bst(nums)\n        return self.perms(root)[1] - 1\n\n    def perms(self, node):\n        """ returns (size, perms) """\n        if node is None:\n            return 0, 1\n        \n        left_size, left_perms = self.perms(node.left)\n        right_size, right_perms = self.perms(node.right)\n        size = left_size + right_size + 1\n\n        res = comb(size-1, left_size) * left_perms * right_perms\n    \n        return size, res % MOD\n    \n    def _build_bst(self, nums):\n        bst = BST()\n        for x in nums:\n            bst.insert(x)\n        return bst.root\n\n\nclass BST:\n    def __init__(self):\n        self.root = None\n    \n    def insert(self, val):\n        self.root = self._insert(self.root, val)\n    \n    def _insert(self, node, val):\n        if node is None:\n            return TreeNode(val)\n\n        if val < node.val:\n            node.left = self._insert(node.left, val)\n        else:\n            node.right = self._insert(node.right, val)\n        return node\n\n\nclass TreeNode:\n    def __init__(self, val):\n        self.val = val\n        self.left = None\n        self.right = None\n```
['Python3']