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
86
87
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;

int cntZ[2];
int cntC[2];
int cntN;
long long pot2[200005];
long long inv3;

long long pot(long long a, int x){
    if(x == 0) return 1;
    if(x&1) return pot(a, x-1)*a%mod;
    long long tmp = pot(a, x/2);
    return tmp*tmp%mod;
}

long long binom_sum(int n, int k){
    k %= 3;
    int a = (n-2*k)%6;
    if(a < 0) a += 6;
    long long res = pot2[n];
    if(a == 0) res+=2;
    else if(a == 1 || a == 5) res+=1;
    else if(a == 3) res-=2;
    else res-=1;
    //cout<<"res "<<res<<endl;
    res = res*inv3%mod;
    if(res < 0) res+=mod;
    //cout<<"binom_sum: "<<n<<" "<<k<<" "<<res<<endl;
    return res;
}

void query(int n){
    int sumZ = cntZ[0]+cntZ[1];
    int sumC = cntC[0]+cntC[1];
    int k = ((sumC-sumZ+cntN)*2)%3;
    long long res = pot2[cntN]-binom_sum(cntN, k);

    if(cntZ[0] == 0 && cntC[1] == 0){
        if(n%2 && n>1) res--;
    }
    if(cntC[0] == 0 && cntZ[1] == 0){
        if(n%2 && n>1) res--;
    }

    res %= mod;
    if(res < 0) res += mod;
    cout<<res<<"\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin>>n>>q;
    string s;
    cin>>s;
    pot2[0] = 1;
    for(int i=1;i<=n;i++){
        pot2[i] = (pot2[i-1]*2)%mod;
    }
    inv3 = pot(3, mod-2);
    
    for(int i=0;i<n;i++){
        if(s[i] == 'N') cntN++;
        else if(s[i] == 'Z') cntZ[i&1]++;
        else cntC[i&1]++;
    }
    query(n);

    while(q--){
        int i;
        char x;
        cin>>i>>x;
        i--;
        if(s[i] == 'N') cntN--;
        else if(s[i] == 'Z') cntZ[i&1]--;
        else cntC[i&1]--;
        s[i] = x;
        if(s[i] == 'N') cntN++;
        else if(s[i] == 'Z') cntZ[i&1]++;
        else cntC[i&1]++;
        query(n);
    }
}