<p>You are given a string <code>s</code> of length <code>n</code> where <code>s[i]</code> is either:</p>

<ul>
	<li><code>&#39;D&#39;</code> means decreasing, or</li>
	<li><code>&#39;I&#39;</code> means increasing.</li>
</ul>

<p>A permutation <code>perm</code> of <code>n + 1</code> integers of all the integers in the range <code>[0, n]</code> is called a <strong>valid permutation</strong> if for all valid <code>i</code>:</p>

<ul>
	<li>If <code>s[i] == &#39;D&#39;</code>, then <code>perm[i] &gt; perm[i + 1]</code>, and</li>
	<li>If <code>s[i] == &#39;I&#39;</code>, then <code>perm[i] &lt; perm[i + 1]</code>.</li>
</ul>

<p>Return <em>the number of <strong>valid permutations</strong> </em><code>perm</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;DID&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> The 5 valid permutations of (0, 1, 2, 3) are:
(1, 0, 3, 2)
(2, 0, 3, 1)
(2, 1, 3, 0)
(3, 0, 2, 1)
(3, 1, 2, 0)
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;D&quot;
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == s.length</code></li>
	<li><code>1 &lt;= n &lt;= 200</code></li>
	<li><code>s[i]</code> is either <code>&#39;I&#39;</code> or <code>&#39;D&#39;</code>.</li>
</ul>

class Solution {
public:
    using ll = long long;
    const int mod = 1e9+7;
    int numPermsDISequence(string s) {
        // for(auto& c:s){
        //     if(c=='D') c='I';
        //     else c='D';
        // }
        int n = s.size();
        vector<vector<ll>> dp(n+1, vector<ll>(2*(n+1)+1, 0));
        // either use first n or last n
        dp[n][1] = 1;
        dp[n][n+2] = 1;

        for(int i=n-1; i>=0; i--){
            // we have to put n-i+1 elements still
            for(int j=1; j<=n-i+1; j++){
                // here you can use 1....j elements to put here
                for(int k=1; k<=j; k++){
                    // we have put k here
                    // k-1 elements are less than our choice
                    // n-i+1-(k-1)-1 elements are greater
                    if(s[i]=='I'){
                        // next element has to be greater
                        // last choice has different dp state
                        dp[i][j] += dp[i+1][(n+1)+n-i+1-(k-1)-1];
                    }
                    else dp[i][j] += dp[i+1][k-1];
                    dp[i][j] %= mod;
                }
            }

            for(int j=(n+1)+1; j<=(n+1)+n-i+1; j++){
                // we can use last (j-n) elements
                for(int k=1; k<=(j-(n+1)); k++){
                    // we are using the kth last element
                    // (k-1) elements are greater than our choice
                    // n-i+1-(k-1)-1 elements are smaller
                    if(s[i]=='I'){
                        // next element has to be greater
                        dp[i][j] += dp[i+1][(n+1)+k-1];
                    }
                    else dp[i][j] += dp[i+1][n-i+1-(k-1)-1];
                    // if(i==0 && j==4) cout << k << " : " << dp[i][j] << "\n";
                    dp[i][j] %= mod;
                }
            }
        }
        // for(int i=0; i<=n; i++){
        //     for(int j=)
        // }

        // if(s=="DID"){
        //     // cout << dp[2][1] << "\n";
        //     // cout << dp[2][2] << "\n";
        //     // cout << "--------\n";
        //     // cout << dp[2][3+1] << "\n";
        //     // cout << dp[2][3+2] << "\n";

        //     cout << dp[1][1] << "\n";
        //     cout << dp[1][2] << "\n";
        //     cout << dp[1][3] << "\n";
        //     cout << "--------\n";
        //     cout << dp[1][3+1] << "\n";
        //     cout << dp[1][3+2] << "\n";
        //     cout << dp[1][3+3] << "\n";

        //     cout << "***********\n";
        //     cout << dp[0][1] << "\n";
        //     cout << dp[0][2] << "\n";
        //     cout << dp[0][3] << "\n";
        //     cout << dp[0][4] << "\n";
        //     cout << "--------\n";
        //     cout << dp[0][3+1] << "\n";
        //     cout << dp[0][3+2] << "\n";
        //     cout << dp[0][3+3] << "\n";
        //     cout << dp[0][3+4] << "\n";

        // }
        return dp[0][n+1];
    }
};
12500