1569. Number of Ways to Reorder Array to Get Same BST

nums is a permutation of 1..n. Inserting its elements in order into an empty BST gives some tree. Return how many other orderings of nums produce the exact same BST, modulo 10^9 + 7.

Patterns: recursion, divide-and-conquer, bst. Difficulty: hard.

Open in Dryrun