1
# Runtime: 556 ms (Top 18.24%) | Memory: 15.4 MB (Top 89.19%)3
def __init__(self, val):10
def BST(self, root, cur):11
if cur.val < root.val:16
self.BST(root.left, cur)17
elif cur.val > root.val:18
if root.right == None:22
self.BST(root.right, cur)24
def solve(self, root):25
if root.left == None and root.right == None:29
if root.left is not None:30
left = self.solve(root.left)31
if root.right is not None:32
right = self.solve(root.right)33
self.total *= math.comb(left + right, left)34
return left + right + 136
def numOfWays(self, nums: List[int]) -> int:41
for i in range(1, len(nums)):42
self.BST(root, node(nums[i]))44
return (self.total - 1) % (int(1e9) + 7)