<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)
        matrix = [[0 for _ in range(n)] for __ in range(n)]
        print('aaa')

        for i in range(n):
            matrix[i][i] = 1

        for i in range(n-1):
            matrix[i][i+1] = 2


        for length in range(3,n+1):
            for i in range(0,n-length+1):
                j = i+length-1
                if s[i] == s[j]:
                    left = i+1
                    right = j-1
    
                    while left <= j:
                        if s[left] != s[i]:
                            left +=1
                        else:
                            break
                    
                    while right >= i:
                        if s[right] != s[i]:
                            right -=1 
                        else:
                            break
                    
                    if left > right:
                        matrix[i][j] = 2*matrix[i+1][j-1] + 2
                    elif left == right:
                        matrix[i][j] = 2*matrix[i+1][j-1] + 1
                    else:
                        matrix[i][j] = 2*matrix[i+1][j-1] - matrix[left+1][right-1]

                else:
                    matrix[i][j] = matrix[i+1][j] + matrix[i][j-1] - matrix[i+1][j-1]

        # print('matrix',matrix)
        return matrix[0][n-1]%(10**9+7)

# Base Cases:
# If i > j, it means the substring is empty, so the function returns 0.
# If i == j, it means the substring has only one character, which is a palindrome itself. Hence, it returns 1.
# If the result for indices (i, j) is already computed (memoized), it's returned directly to avoid re-computation.

# Recursive Case:
# If s[i] == s[j], it means the two ends of the current substring are the same.
# In this case, find the next occurrence of s[i] after i and the previous occurrence of s[j] before j. These are represented by l and r, respectively.
# If l > r, it means there are no characters the same as s[i] or s[j] between i and j. So, the count is 2 * helper(i+1, j-1) + 2. The additional 2 is for counting the subsequences formed by only s[i] and the one formed by both s[i] and s[j].
# If l == r, it means there's only one occurrence of the character. So, the count becomes 2 * helper(i+1, j-1) + 1.
# Otherwise, there are at least two occurrences of the character between i and j. In this case, we need to subtract the palindromic subsequences counted in between l and r to avoid over-counting.
# If s[i] != s[j], the count becomes the sum of palindromic subsequences in the substring (i+1, j) and the substring (i, j-1). We subtract the count from substring (i+1, j-1) to avoid counting the same subsequences twice.

# Memoization:
# Before returning the count, we store it in the memo dictionary to avoid redundant computations in future calls.

# Return:
# The function returns the count of different non-empty palindromic subsequences for the substring represented by indices i and j.
46318