<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)
        MOD = 10**9 + 7
        dp = [[0] * n for _ in range(n)]
        next_char = [[-1] * 4 for _ in range(n)]
        prev_char = [[-1] * 4 for _ in range(n)]
        
        # Precompute the next and previous occurrence of each character
        for i in range(n):
            for j in range(4):
                prev_char[i][j] = next_char[i][j] = -1
            if i > 0:
                for j in range(4):
                    prev_char[i][j] = prev_char[i - 1][j]
            prev_char[i][ord(s[i]) - ord('a')] = i
        
        for i in range(n - 1, -1, -1):
            for j in range(4):
                next_char[i][j] = next_char[i + 1][j] if i + 1 < n else -1
            next_char[i][ord(s[i]) - ord('a')] = i
        
        # Function to compute the number of palindromic subsequences
        def count(i, j):
            if i > j:
                return 0
            if i == j:
                return 1
            if dp[i][j] != 0:
                return dp[i][j]
            
            dp[i][j] = 0
            for k in range(4):
                left = next_char[i][k]
                right = prev_char[j][k]
                if left != -1 and right != -1 and left <= j:
                    if left == right:
                        dp[i][j] += 1
                    else:
                        dp[i][j] += count(left + 1, right - 1) + 2
            dp[i][j] %= MOD
            return dp[i][j]
        
        return count(0, n - 1)

# Example usage:
solution = Solution()
print(solution.countPalindromicSubsequences("bccb"))  # Output: 6
print(solution.countPalindromicSubsequences("abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba"))  # Output: 104860361
52595