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
#include<bits/stdc++.h>
using namespace std;
int MOD = 1e9 + 7;
int n, N, C, Z, q;
int p[5],b[1000005][5], ile[5][5];
char c[1000005];
void odp(){
    long long odp = 0;
    for(int i = 0 ; i <= p[0] ; i++){
        if((p[1] + i) % 3 == (p[2] +p[0] - i) % 3){
            if(i == 0){
                odp = b[p[0]][1];
                if(ile[1][0] == 0 && ile[2][1] == 0 && ile[0][1] % 3 != 0)
                    odp--;
                if(ile[1][1] == 0 && ile[2][0] == 0 && ile[0][0] % 3 != 0)
                    odp--;
                break;
            }
            else if(i == 1){
                odp = b[p[0]][2];
                 if(ile[1][0] == 0 && ile[2][1] == 0 && ile[0][1] % 3 != 1)
                    odp--;
                if(ile[1][1] == 0 && ile[2][0] == 0 && ile[0][0] % 3 != 1)
                    odp--;
                break;
            }
            else{
                odp = b[p[0]][3];
                 if(ile[1][0] == 0 && ile[2][1] == 0 && ile[0][1] % 3 != 2)
                    odp--;
                if(ile[1][1] == 0 && ile[2][0] == 0 && ile[0][0] % 3 != 2)
                    odp--;
                break;
            }
        }
    }
    cout << odp<<"\n";
}

int fun(char x){
    if(x == 'N')
        return 0;
    if(x == 'C')
        return 1;
    return 2;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> q;
    b[0][1] = 0;
    b[0][2] = 1;
    b[0][3] = 1; 

    for(int i = 1 ; i <= n ; i++){
        b[i][1] = (b[i-1][1] + b[i-1][3]) % MOD;
        b[i][2] = (b[i-1][2] + b[i-1][1]) % MOD;
        b[i][3] = (b[i-1][3] + b[i-1][2]) % MOD;
    }

    for(int i = 1 ; i <= n ; i++){
        cin >> c[i];
        int w = fun(c[i]);
        p[w]++;
        ile[w][i%2]++;
    }
    odp();
    for(int i = 1 ; i <= q ; i++){
        int x;
        char ch;

        cin >> x >> ch;
        int w = fun(c[x]);
        int y = fun(ch);
        p[w]--;
        p[y]++;

        ile[w][x%2]--;
        ile[y][x%2]++;
        
        c[x] = ch;
        odp();
    }
}