#include #pragma GCC optimize("Ofast") #define _GLIBCXX_DEBUG using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; const ll mod=1e9+7; const ll mod_half=5e8+4; #define rep(i,a) for (ll i=0;i std::vector Z_algo(std::vector &vec){ int n=vec.size(); int ind=1,j=0,k; std::vector ans(n,0); ans[0]=n; while(ind Z_algo(std::string &vec){ int n=vec.size(); int ind=1,j=0,k; std::vector ans(n,0); ans[0]=n; while(ind>N; vector A(N+1); vector sum_A(N); rep(i,N){ cin>>A[i]; if(i) sum_A[i]=(sum_A[i-1]+A[i])%mod; else sum_A[i]=A[i]; } string S; cin>>S; assert(S.size()==N); S+="#"; vector> p(N); rep(i,N-1){ p[i]={S[i+1],A[i+1]}; } auto z_val=Z_algo(p); ll ans=0; rep(i,N){ if(S[i]!=S[0]) continue; if(A[0]>A[i]){ ans+=((((A[i]+1)*A[i])%mod)*mod_half)%mod; ans%=mod; }else{ ans+=(((A[0]*(A[i]*2-A[0]+1))%mod)*mod_half)%mod; ans%=mod; if(i==0){ ans+=sum_A[N-1]-A[0]; ans%=mod; continue; } ans+=sum_A[z_val[i]]-A[0]; if(S[z_val[i]+1]==S[z_val[i]+1+i]){ ans+=min(A[z_val[i]+1],A[z_val[i]+i+1]); ans%=mod; } } } cout<<(ans%mod+mod)%mod<<"\n"; }