import sys
input = sys.stdin.readline
#f=open('wztmax.in', 'r')
#fw=open('wztm.out','w')
#sys.stdout = fw
#input=f.readline
n,q=list(map(int,input().split()))
t=list(input())
d=10**9+7
r=[]
a,b=1,0
for i in range(n+1):
a,b=(2*b)%d,(a+b)%d
r.append([a,b])
rr=0
nn=0
tt=[0,0]
i=0
for s in t:
if s=="N":
nn+=1
elif s=="C":
rr+=1
tt[i]+=1
elif s=="Z":
rr+=2
tt[1-i]+=1
i=1-i
rr=rr%3
rd=min(rr,1)
p=r[nn][rd]
if n%2==1:
if tt[0]==0:
p-=1
if tt[1]==0:
p-=1
print((p+d)%d)
for i in range(q):
jj,c=input().split()
j=int(jj)-1
if c!=t[j]:
if c=="N":
if t[j]=="C":
rr+=2
tt[j%2]-=1
else:
rr+=1
tt[(j+1)%2]-=1
nn+=1
elif c=="C":
if t[j]=="N":
nn-=1
tt[j%2]+=1
rr+=1
else:
rr+=2
tt[j%2]+=1
tt[(j+1)%2]-=1
else:
if t[j]=="N":
nn-=1
tt[(j+1)%2]+=1
rr+=2
else:
rr+=1
tt[j%2]-=1
tt[(j+1)%2]+=1
t[j]=c
rr=rr%3
rd=min(rr,1)
p=r[nn][rd]
if n%2==1:
if tt[0]==0:
p-=1
if tt[1]==0:
p-=1
print((p+d)%d)
#f.close()
#fw.close()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import sys input = sys.stdin.readline #f=open('wztmax.in', 'r') #fw=open('wztm.out','w') #sys.stdout = fw #input=f.readline n,q=list(map(int,input().split())) t=list(input()) d=10**9+7 r=[] a,b=1,0 for i in range(n+1): a,b=(2*b)%d,(a+b)%d r.append([a,b]) rr=0 nn=0 tt=[0,0] i=0 for s in t: if s=="N": nn+=1 elif s=="C": rr+=1 tt[i]+=1 elif s=="Z": rr+=2 tt[1-i]+=1 i=1-i rr=rr%3 rd=min(rr,1) p=r[nn][rd] if n%2==1: if tt[0]==0: p-=1 if tt[1]==0: p-=1 print((p+d)%d) for i in range(q): jj,c=input().split() j=int(jj)-1 if c!=t[j]: if c=="N": if t[j]=="C": rr+=2 tt[j%2]-=1 else: rr+=1 tt[(j+1)%2]-=1 nn+=1 elif c=="C": if t[j]=="N": nn-=1 tt[j%2]+=1 rr+=1 else: rr+=2 tt[j%2]+=1 tt[(j+1)%2]-=1 else: if t[j]=="N": nn-=1 tt[(j+1)%2]+=1 rr+=2 else: rr+=1 tt[j%2]-=1 tt[(j+1)%2]+=1 t[j]=c rr=rr%3 rd=min(rr,1) p=r[nn][rd] if n%2==1: if tt[0]==0: p-=1 if tt[1]==0: p-=1 print((p+d)%d) #f.close() #fw.close() |
polski