#include #include #include using namespace atcoder; using mint = modint1000000007; using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define Inf 1000000001 struct rolling_hash{ long long t_hash; static vector power; static const long long MOD = (1LL<<61)-1; static const long long b = 123456; int sz; rolling_hash(){ sz = 0; t_hash = 0; } rolling_hash(long long c){ sz = 1; t_hash = b*c; } long long mul(__int128 x,__int128 y){ __int128 t = x*y; t = (t>>61) + (t&MOD); if(t>=MOD)t -= MOD; return t; } long long get_pow(int sz){ if(power.size()>sz)return power[sz]; while(power.size()<=sz){ if(power.size()==0)power.push_back(1); else power.push_back(mul(power.back(),b)); } return power.back(); } rolling_hash &operator+=(const rolling_hash &another){ (*this).t_hash = mul((*this).t_hash,get_pow(another.sz)); (*this).t_hash += another.t_hash; if((*this).t_hash>=MOD)(*this).t_hash -= MOD; (*this).sz += another.sz; return (*this); } rolling_hash operator+(const rolling_hash &another)const{ return (rolling_hash(*this)+=another); } rolling_hash &operator-=(const rolling_hash &another){ (*this).t_hash += MOD - mul(another.t_hash,get_pow((*this).sz-another.sz)); if((*this).t_hash>=MOD)(*this).t_hash -= MOD; (*this).sz -= another.sz; return (*this); } rolling_hash operator-(const rolling_hash &another)const{ return (rolling_hash(*this)-=another); } bool operator<(const rolling_hash &another)const{ if((*this).t_hash!=another.t_hash)return (*this).t_hash rolling_hash::power; mint sum(long long n){ mint ret = n; ret *= n+1; ret /= 2; return ret; } int main(){ int n; cin>>n; vector a(n); rep(i,n)cin>>a[i]; string s; cin>>s; { vector b; string t = ""; rep(i,n){ if(t.size()==0 || t.back()!=s[i]){ b.push_back(a[i]); t += s[i]; } else{ b.back() += a[i]; } } a = b; s = t; n = a.size(); } vector R(n+1); rep(i,n){ long long t = s[i]; t *= a[i]; R[i+1] = rolling_hash(t); R[i+1] = R[i] + R[i+1]; } vector SS(n+1,0); rep(i,n){ SS[i+1] = SS[i] + a[i]; } mint ans = 0; rep(i,n){ if(s[i] != s[0])continue; if(a[i] < a[0]){ ans += sum(a[i]); } else{ ans += sum(a[0]); ans -= a[0]; ans += a[0] * (a[i] - a[0]); int ok = 1,ng = n+1; while(ng-ok>1){ int mid = (ok+ng)/2; if(i+mid>n)ng = mid; else if(R[mid] - R[1] == R[i+mid] - R[i+1])ok = mid; else ng= mid; } mint temp = a[0] + SS[ok] - SS[1]; if(i+ok