<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 {
public:
    long long dp[1002][1002];
    const long long mod = 1e9+7;
    map<int,set<int>> next,prev;
    int n;
    long long rec(long long i,long long j,string &s){
        //cout<<s<<endl;
        if(i>=j){
            return 0;
        }
        if(dp[i][j]!=-1){
            return dp[i][j];
        }

        long long ans = 0;
        
        for(int k=0;k<4;k++){
            char c = k + 'a';
            auto it = next[c].upper_bound(i);
            auto it1 = prev[c].upper_bound(-1*j);
            //cout<<c<<1<<endl;
            if(it==next[c].end() || it1==prev[c].end()){
                ans = (ans+rec(n,1,s))%mod;
                continue;
            }
            if(*(it)<(-1)*(*it1)){
            //    cout<<(*it)<<" "<<(*it1)<<endl;
              ans = (ans + 1 + rec((*it),(-1)*(*it1),s))%mod;
            }
            if((*it)>i && (*it)<j){
                ans = (ans + 1)%mod;
            }
        }
        dp[i][j] = ans;
        return ans;
    }

    int countPalindromicSubsequences(string s) {
        //s[i]  is a,b,c,d
        //why only 4 are given?? bitmask  bcbdabcb
        //if there is no b after this b all will be new
        //dp[i][j] = dp[i-1][j]; dp[i][j-1]; if same take them if not same do what you want to do
        //but all differnet index are counted 
        //what are counted more than once
        memset(dp,-1,sizeof(dp));
         n = s.length();
        for(int i=0;i<n;i++){
            next[s[i]].insert(i+1);
            prev[s[i]].insert(-1*i-1);
        }
        return rec(0,(int)(s.length())+1,s);
    }
};
21050