Dynamic programming with top to down approach
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long mod = 1000000007;\n    int helper(int index, string s, vector<int>&visited, int currentElement, vector<vector<int> >&dp) {\n\n        if(index == s.length()) {\n            return 1;\n        }\n\n        if(dp[index][currentElement] != -1)\n            return dp[index][currentElement];\n        int ans = 0;\n\n        for(int i=0; i<=s.length(); i++) {\n            if(visited[i] == 0) {\n                if(s[index] == \'D\' && currentElement > i || (s[index] == \'I\' && currentElement < i)) {\n                    visited[i] = 1;\n                    ans = (ans + helper(index+1, s, visited, i, dp) ) % mod;\n                    visited[i] = 0;\n                }\n            }\n        }\n\n        dp[index][currentElement] = ans;\n        return ans;\n    }\n    int numPermsDISequence(string s) {\n        int n = s.length();\n        vector<int> visited(n+1, 0);\n        vector<vector<int> > dp(n+1, vector<int>(n+1, -1));\n        int ans = 0;\n        for(int i=0; i<=n; i++) {\n            visited[i] = 1;\n            ans = (ans + helper(0, s, visited, i, dp))%mod;\n            visited[i] = 0;\n        } \n        return ans;\n    }\n};\n```
['C++']