link
class Solution {
public:
#define LL long long
const int mod=1E9+7;
vector<vector<LL>> table;
int numOfWays(vector<int>& nums) {
int n=nums.size();
table.resize(n+1);
for(int i=0;i<=n;i++){
table[i]=vector<LL> (i+1,1);
for(int j=1;j<i;j++){
table[i][j]=(table[i-1][j]+table[i-1][j-1])%mod;
}
}
return dfs(nums)-1;
}
LL dfs(vector<int>& nums){
int n=nums.size();
if(n<=2) return 1;
vector<int> leftarr, rightarr;
for(int i=1;i<n;i++){
if(nums[i]>nums[0]) rightarr.push_back(nums[i]);
else leftarr.push_back(nums[i]);
}
LL leftres=dfs(leftarr);
LL rightres=dfs(rightarr);
return (table[n-1][leftarr.size()]*leftres%mod) * rightres %mod;
}
};