class Solution { public: #define ll long long int mod=1E9+7; int uniqueLetterString(string S) { int n=S.size(); ll res=0; vector<int> last1(26,-1); vector<int> last2(26,-1); for(int i=0;i<n;++i){ char c=S[i]; res+=(last2[c-'A']-last1[c-'A'])*(i-last2[c-'A'])%mod; res%=mod; last1[c-'A']=last2[c-'A']; last2[c-'A']=i; } for(int i=0;i<26;++i){ if(last2[i]!=-1){ res+=(last2[i]-last1[i])*(n-last2[i])%mod; res%=mod; } } return res; } };