<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>

// https://leetcode.com/problems/valid-permutations-for-di-sequence/solutions/168612/top-down-with-memo-bottom-up-dp-n-3-dp-n-2-dp-o-n-space/

class Solution {
public:

    // O(n^2) with prefix sum
    int numPermsDISequence(const std::string& s) {
        int n = s.length(), M = 1000000007;
        std::vector<std::vector<int>> dp(n + 1, std::vector<int>(n + 1));

        dp[0][0] = 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= i; ++j) {
                if (j == 0) {
                    dp[i][j] = 0;
                } else {
                    dp[i][j] = dp[i][j - 1];
                }

                if (s[i - 1] == 'D') {
                    int prev_value = j == 0 ? 0 : dp[i - 1][j - 1];
                    dp[i][j] += (dp[i - 1][i - 1] - prev_value + M) % M; // avoid negative
                } else {
                    if (j == 0) {
                        dp[i][j] = 0;
                    } else {
                        dp[i][j] += dp[i - 1][j - 1];
                    }
                }

                dp[i][j] %= M;
            }
        }

        return dp[n][n];
    }

    // O(N^3)
    // int numPermsDISequence(string s) {
    //     int n = s.length();
    //     const int M = 1e9 + 7;
    //     // dp[i][j] represents the number of permutation of number 0, 1, ... , i which ends with j.
    //     // Also, it represents the answer of s.substring(0, i) which ends with j.
    //     std::vector<std::vector<int>> dp(n + 1, std::vector<int>(n + 1, 0));
    //     dp[0][0] = 1;

    //     for (int i = 1; i <= n; i++) {
    //         for (int j = 0; j <= i; j++) {
    //             if (s[i - 1] == 'D') {
    //                 for (int k = j; k < i; k++) {  // suffix sum
    //                     dp[i][j] = (dp[i][j] + dp[i - 1][k]) % M;
    //                 }
    //             } else { 
    //                 for (int k = 0; k < j; k++) {  // prefix sum
    //                     dp[i][j] = (dp[i][j] + dp[i - 1][k]) % M;
    //                 }
    //             }
    //         }
    //     }

    //     int res = 0;
    //     for (int j = 0; j <= n; j++) {
    //         res = (res + dp[n][j]) % M;
    //     }
    //     return res;
    // }

};
9100