<p>Given a string s, return <em>the number of different non-empty palindromic subsequences in</em> <code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>A subsequence of a string is obtained by deleting zero or more characters from the string.</p>

<p>A sequence is palindromic if it is equal to the sequence reversed.</p>

<p>Two sequences <code>a<sub>1</sub>, a<sub>2</sub>, ...</code> and <code>b<sub>1</sub>, b<sub>2</sub>, ...</code> are different if there is some <code>i</code> for which <code>a<sub>i</sub> != b<sub>i</sub></code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;bccb&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> The 6 different non-empty palindromic subsequences are &#39;b&#39;, &#39;c&#39;, &#39;bb&#39;, &#39;cc&#39;, &#39;bcb&#39;, &#39;bccb&#39;.
Note that &#39;bcb&#39; is counted only once, even though it occurs twice.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba&quot;
<strong>Output:</strong> 104860361
<strong>Explanation:</strong> There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10<sup>9</sup> + 7.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s[i]</code> is either <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, <code>&#39;c&#39;</code>, or <code>&#39;d&#39;</code>.</li>
</ul>

class Solution:
    def countPalindromicSubsequences(self, s: str) -> int:
        
        n = len(s)
        dp = [[0] * n for _ in range(n)]

        # s[i] itself is a palindrome
        for i in range(n):
            dp[i][i] = 1

        for length in range(1, n):
            for start in range(n - length):
                end = start + length

                if s[start] != s[end]:
                    dp[start][end] = dp[start + 1][end] + dp[start][end - 1] - dp[start + 1][end - 1]
                else: # s[start] == s[end]:
                    # Consider the distanct char than s[start] between start + 1 and end - 1
                    l = start + 1
                    r = end - 1
                    while l <= r and s[l] != s[start]:
                        l += 1
                    while l <= r and s[r] != s[start]:
                        r -= 1
                    if l > r: 
                        # every char is different so only need to count s[start] and s[start]s[end]
                        # (c-------------c)
                        dp[start][end] = 2 * dp[start + 1][end - 1] + 2
                    elif l < r:
                        # (c----c----c---c) = a-----s'-----a
                        # remove double counted from 2 * dp[start + 1][end - 1]. note s[start] and s[start]s[end] is part of dp[start + 1][end - 1].
                        dp[start][end] = 2 * dp[start + 1][end - 1] - dp[l + 1][r - 1]
                    else: # l == r
                        # (c------c------c)
                        # s[start] exists once in s[start+1:end]. count s[start]s[end]. note s[start] is part of dp[start + 1][end - 1].
                        dp[start][end] = 2 * dp[start + 1][end - 1] + 1
                dp[start][end] %= (10**9+7)
        
        return dp[0][-1]

24346