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
#include <bits/stdc++.h>

const int MOD = 1e9+7;
const int MAX_N = 2e5;
int n, q;

int f[MAX_N+3][3][3];
int cnt[2][2];
int cnt_n = 0;
std::string S;

void calc_f() {
    f[0][0][0] = 1;
    for (int i = 1; i <= n; i++) 
        for (int x = 0; x < 3; x++) 
            for (int y = 0; y < 3; y++) 
                if (((x+y)%3) == (i%3))
                    f[i][x][y] = (f[i-1][(x+2)%3][y] + f[i-1][x][(y+2)%3]) % MOD;
}

void update(int v, char c) {
    if (S[v] == 'C') cnt[0][v%2]--;
    if (S[v] == 'Z') cnt[1][v%2]--;
    if (S[v] == 'N') cnt_n--;

    if (c == 'C') cnt[0][v%2]++;
    if (c == 'Z') cnt[1][v%2]++;
    if (c == 'N') cnt_n++;

    S[v] = c;
}

void ans() {
    int res = 0;
    int cnt0 = cnt[0][0] + cnt[0][1];
    int cnt1 = cnt[1][0] + cnt[1][1];
    for (int x = 0; x < 3; x++) 
        for (int y = 0; y < 3; y++) 
            if (((cnt0+x)%3) != ((cnt1+y)%3)) 
                res = (res + f[cnt_n][x][y]) % MOD;

    // the weird cases
    if (cnt_n < n && n > 1 && (n%2) == 1 && 
        ((cnt[0][0] == 0 && cnt[1][1] == 0) ||
         (cnt[0][1] == 0 && cnt[1][0] == 0))
    ) res = (res + MOD - 1) % MOD;
    if (cnt_n == n && n > 1 && (n%2) == 1) 
        res = (res + MOD - 2) % MOD;

    std::cout << res << "\n";
}

int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);
    std::cin >> n >> q >> S;
    for (int i = 0; i < n; i++) {
        if (S[i] == 'C') cnt[0][i%2]++;
        if (S[i] == 'Z') cnt[1][i%2]++;
        if (S[i] == 'N') cnt_n++;
    }
    calc_f();
    ans();
    int v; char c;
    while (q--) {
        std::cin >> v >> c;
        update(v-1, c);
        ans();
    }
}