Easy and commented [C++] solution
\n#define ll long long\n\nclass Solution {\npublic:\n    \n    ll MOD=1e9L+7;\n    ll dp[1001][1001]; // dp[i][j] = iCj (i choose j)\n    \n    ll util(vector<i
```\n#define ll long long\n\nclass Solution {\npublic:\n    \n    ll MOD=1e9L+7;\n    ll dp[1001][1001]; // dp[i][j] = iCj (i choose j)\n    \n    ll util(vector<int>& v){\n        int n=v.size();\n        if(n==0) return 1; // base case when binary search tree is empty\n        \n        int root=v[0]; // root of the binary search tree\n        \n        vector<int> left, right;\n        \n        for(int i=1; i<n; i++){\n            if(v[i]>root){\n                right.push_back(v[i]); // right children of root\n            }\n            else if(v[i]<root){\n                left.push_back(v[i]); // left children of root\n            }\n        }\n        \n        ll left_ans=util(left); // number of ways to form left subtree\n        ll right_ans=util(right); // number of ways to form right subtree\n        \n        ll ans=(ll)left_ans*right_ans%MOD; // merging the answer of left and right (standard divide conquer strategy)\n        \n        ans=(ll)ans*dp[n-1][(int)left.size()]%MOD; // also among total n-1 insertions in this tree there are (n-1)C(left.size()) ways to interleave the left and right insertions\n        \n        return ans;\n    }\n    \n    int numOfWays(vector<int>& nums) {\n        dp[0][0]=1;\n        \n        for(ll i=1; i<=nums.size(); i++){\n            dp[i][0]=1;\n            dp[i][i]=1;\n            for(ll j=1; j<i; j++){\n                dp[i][j]=(dp[i-1][j-1]+dp[i-1][j])%MOD;\n            }\n        }\n        \n        ll ans=util(nums);\n        \n        ans=(ans+MOD-1)%MOD; // need to subtract the already given ordering\n        \n        return ans; // :)\n    }\n};\n```
[]