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

using vi = vector<int>;
using vvi = vector<vi>;
class Solution {
public:
    int n;
    int mod = 1e9 + 7;
    vvi first;
    vvi last;
    vvi freq;

    int find_freq(int i, int j, int c){
        int ans = freq[j][c];
        if(i) ans -= freq[i-1][c];
        return ans;
    }   

    void precomp(string s){
        // cout<<n;
        freq.assign(n, vi(26, 0));

        // cout<<freq.size();
        freq[0][s[0] - 'a'] = 1;

        for(int i=1; i<n; i++){
            for(int c=0; c<26; c++){
                freq[i][c] = freq[i-1][c];
                if(s[i] == 'a' + c) freq[i][c] += 1;
            }
        }


        first.assign(n, vi(26, 0)); // next occ of letter c from index i given by first[i]['c' - a]
        last.assign(n, vi(26, 0)); // prev occ

        for(int i=0; i<26; i++){
            int occ = -1;

            for(int j=0; j<n; j++){
                if(s[j] == 'a' + i) occ = j;
                last[j][i] = occ;
            }
        }

        for(int i=25; i>=0; i--){
            int occ = -1;

            for(int j=n-1; j>=0; j--){
                if(s[j] == 'a' + i) occ = j;
                first[j][i] = occ;
            }
        }

    }

    int countPalindromicSubsequences(string s) {

        n = s.length();

        precomp(s);

        
        vvi dp(n, vi(n, 0));

        // for(int i=0; i<n; i++){
        //     cout<<i<<"\n";
        //     for(int c=0; c<26; c++) cout<<freq[i][c];
        //     cout<<"\n";
        // }

        for(int i=0; i<n; i++) dp[i][i] = 1;
        for(int i=0; i<n-1; i++) dp[i][i+1] = 2;

        for(int i=n-3; i>=0; i--){
            // cout<<i;
            for(int j=i+2; j<n; j++){

                // if different chars

                if(s[i] != s[j]){
                    dp[i][j] = (dp[i+1][j]%mod + (dp[i][j-1]%mod + (mod - dp[i+1][j-1]%mod)%mod)%mod)%mod;
                    // dp[i][j] = (dp[i+1][j]%mod + (dp[i][j-1]%mod + mod - dp[i+1][j-1]%mod)%mod)%mod; 
                }

                else{

                    int nums = find_freq(i+1, j-1, s[i] - 'a');
                    // if(i == 0 && j == n-1) cout<<"fr"<<nums<<"\n"; 

                    if(!nums){
                        dp[i][j] = (dp[i+1][j-1]%mod + (dp[i+1][j-1]%mod + 2)%mod)%mod;
                    }
                    else if(nums == 1){
                        dp[i][j] = (dp[i+1][j-1]%mod + (dp[i+1][j-1]%mod + 1)%mod)%mod;
                    }
                    else{
                        // dp[i][j] = (dp[i+1][j-1]%mod + (dp[i+1][j-1]%mod - dp[first[i+1][s[i]-'a']][last[j-1][s[i]-'a']]%mod + mod)%mod)%mod;
                        // cout<<first[i+1][s[i]-'a']<<" "<<last[j-1][s[i]-'a']<<"\n";
                        dp[i][j] = (2*dp[i+1][j-1]%mod + (mod - dp[first[i+1][s[i]-'a']+1][last[j-1][s[i]-'a']-1]%mod)%mod)%mod;
                    }

                }

            }
        }

        // for(auto v: dp){
        //     for(auto e: v) cout<<e<<" ";
        //     cout<<"\n";
        // }

        return dp[0][n-1];
        
    }
};
43423