#include<iostream>
using namespace std;
#define m 1000000007
long long n,nn,q,k1[500010],k0[500010],l,r,x,xx,p[1048580],cnt1,cnt2,lowbit,highbit,t1,t2,qc;
string s;
int pl(int p1,int ls,int rs,int fl,int fr){
if(ls==fl && rs==fr) return p[p1];
if(fl>(ls+rs)/2) return pl((p1<<1)^1,(ls+rs+1)/2,rs,fl,fr);
if(fr<=(ls+rs)/2) return pl(p1<<1,ls,(ls+rs)/2,fl,fr);
int re1=pl((p1<<1)^1,(ls+rs+1)/2,rs,(ls+rs+1)/2,fr),re2=pl(p1<<1,ls,(ls+rs)/2,fl,(ls+rs)/2);
if(k0[re1]-k1[re1]<=k0[re2]-k1[re2]) return re2;
return re1;
}
int main(){
cin>>n>>q;
cin>>s;
for(int i=0;i<n;i++) k1[i+1]=k1[i]+(s[i]^48);
for(int i=0;i<n;i++) k0[i+1]=k0[i]+(s[i]^49);
k1[n+1]=600000;
nn=n-1;
while(nn^(nn&(-nn))) nn^=(nn&(-nn));
nn<<=1;
for(int i=nn;i<nn+n;i++) p[i]=i-nn+1;
for(int i=nn+n;i<(nn<<1);i++) p[i]=n+1;
for(int i=nn-1;i;i--) p[i]=(k0[p[i<<1]]-k1[p[i<<1]]<k0[p[(i<<1)^1]]-k1[p[(i<<1)^1]])?p[(i<<1)^1]:p[i<<1];
while(q--){
qc++;
cin>>l>>r>>x;
xx=x;
lowbit=0;
highbit=0;
while(!(xx&1)){
xx>>=1;
lowbit++;
}
highbit=lowbit;
cnt1=0;
cnt2=0;
while(xx){
if(xx&1) cnt2++;
else cnt1++;
xx>>=1;
highbit++;
}
cnt2-=2;
highbit--;
if(!(x&(x-1))){
t1=1;
t2=2;
highbit+=(r-l+1);
while(highbit){
if(highbit&1) t1=(t1*t2)%m;
highbit>>=1;
t2=(t2*t2)%m;
}
cout<<t1<<endl;
}else if(cnt1>=k0[r]-k0[l-1]){
x-=(1<<highbit);
for(int i=0;i<k0[r]-k0[l-1];i++) x+=(x&(-x));
highbit+=k1[r]-k1[l-1];
t1=1;
t2=2;
while(highbit){
if(highbit&1) t1=(t1*t2)%m;
highbit>>=1;
t2=(t2*t2)%m;
}
x=(x+t1)%m;
cout<<x<<endl;
}else{
t1=pl(1,1,nn,l,r);
if(k0[t1]-k1[t1]-k0[l-1]+k1[l-1]<=cnt1){
lowbit+=cnt2;
lowbit+=k0[r]-k0[l-1];
highbit+=k1[r]-k1[l-1];
t1=1;
t2=2;
while(highbit){
if(highbit&1) t1=(t1*t2)%m;
highbit>>=1;
t2=(t2*t2)%m;
}
x=t1;
t1=1;
t2=2;
while(lowbit){
if(lowbit&1) t1=(t1*t2)%m;
lowbit>>=1;
t2=(t2*t2)%m;
}
x=(x+t1)%m;
cout<<x<<endl;
}else{
int rr=r,ll=l;
r=t1;
l--;
while(l+1<r){
int mid=(l+r)/2;
int mid1=pl(1,1,nn,l+1,mid);
if(k0[mid1]-k1[mid1]-k0[ll-1]+k1[ll-1]<=cnt1) l=mid;
else r=mid;
}
highbit+=(k1[r-1]-k1[ll-1]+1+rr-r);
t1=1;
t2=2;
while(highbit){
if(highbit&1) t1=(t1*t2)%m;
t2=(t2*t2)%m;
highbit>>=1;
}
cout<<t1<<endl;
}
}
}
return 0;
}
这不是O(qlogn)的吗?哪里炸了?