<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:
        def dfs(i, j):

            # base1; infeasible
            if i > j:
                return 0

            # base2; one character
            if i == j: 
                return 1
        
            # seen before
            if (i, j) in memo:
                return memo[(i, j)]

            res = 0

            # if the same character
            if s[i] == s[j]:
                # pair (i, j) and recur on s[i + 1: j - 1]
                temp = dfs(i + 1, j - 1)

                # aggregate s[i: j] and s[i + 1 : j - 1]
                res = 2 * temp

                # slide from left and right to find the next indices
                # with the same s[i] in s[i + 1: j - 1]
                left, right = i + 1, j - 1
                while left <= right and s[i] != s[left]: 
                    left += 1

                while left <= right and s[j] != s[right]: 
                    right -= 1

                # remove duplicate palindromes in s[i + 1: j - 1]
                if left < right: 
                    res -= dfs(left + 1, right - 1)

                # add 1 if there is one character overlap
                elif left == right: 
                    res += 1

                # add 2 if s[i] does not exist in s[i + 1: j - 1]; 
                # one for s[i] and one for s[i]s[j]
                elif left > right:
                    res += 2

            # if not the same ch
            else:
                skip_i = dfs(i + 1, j) % kMod
                skip_j = dfs(i, j - 1) % kMod
                skip_i_j = dfs(i + 1, j - 1) % kMod

                # remove intersection for not counting it twice
                res = skip_i + skip_j - skip_i_j

            memo[(i, j)] = res % kMod

            return memo[(i, j)]


        kMod = 10 ** 9 + 7
        n = len(s)

        # memo[(i, j)] := number of different non-empty palindromic subsequences in s[i: j]
        memo = dict()

        return dfs(0, n - 1)
175006