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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <bits/stdc++.h>
#define boost                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define dbg(x) cerr << #x << " = " << x << "\n";

using namespace std;

const int MOD = 1'000'000'007;
int fac[200005], pow_2[200005];

int power(int x, int y) {
    int res = 1;
    int mult = x;
    while (y) {
        if (y & 1) res = 1LL * mult * res % MOD;
        mult = 1LL * mult * mult % MOD;
        y >>= 1;
    }

    return res;
}

int inv(int n) { return power(n, MOD - 2); }
int parity_sum[2];
int inv3 = inv(3);

int choose(int n, int k) {
    int res = 1LL * fac[n] * inv(fac[k]) % MOD;
    res = 1LL * res * inv(fac[n - k]) % MOD;
    return res;
}

// Niech żyje pałowanie.
int offsets[3][6] = {
    {2, 1, -1, -2, -1, 1},
    {-1, 1, 2, 1, -1, -2},
    {-1, -2, -1, 1, 2, 1},
};

int calc_res(int r, int g, int b) {
    int n = r + g + b;
    int res = pow_2[b];
    int offset;

    if (n > 2) {
        for (int i = 0; i <= 3; i++) {
            if (((r + i) - (g + (b - i))) % 3 == 0) {
                offset = i;
                break;
            }
        }

        int triples = pow_2[b] + offsets[offset][b % 6];
        triples %= MOD;

        triples = 1LL * triples * inv3 % MOD;
        res -= triples;
        if (res < 0) res += MOD;
    }
    if (n >= 2 && ((parity_sum[0] == r && parity_sum[1] == -g) ||
                   (parity_sum[0] == -g && parity_sum[1] == r))) {
        if (!(n > 2 && n % 2 == 0)) {
            res -= (r + g > 0 ? 1 : 2);
        }

        if (res < 0) res += MOD;
    }

    return res;
}

int32_t main() {
    boost;

    int n, q;
    cin >> n >> q;

    fac[0] = 1;
    for (int i = 1; i <= n; i++) {
        fac[i] = 1LL * fac[i - 1] * i % MOD;
    }

    pow_2[0] = 1;
    for (int i = 1; i <= n; i++) pow_2[i] = 1LL * 2 * pow_2[i - 1] % MOD;

    int r = 0, g = 0, b = 0;
    string s;
    cin >> s;
    for (int i = 0; i < n; i++) {
        switch (s[i]) {
            case 'N':
                b++;
                break;
            case 'C':
                parity_sum[i % 2]++;
                r++;
                break;
            case 'Z':
                parity_sum[i % 2]--;
                g++;
                break;
        }
    }

    cout << calc_res(r, g, b) << "\n";

    while (q--) {
        int pos;
        char c;
        cin >> pos >> c;
        pos--;

        switch (s[pos]) {
            case 'N':
                b--;
                break;
            case 'C':
                parity_sum[pos % 2]--;
                r--;
                break;
            case 'Z':
                parity_sum[pos % 2]++;
                g--;
                break;
        }

        s[pos] = c;

        switch (s[pos]) {
            case 'N':
                b++;
                break;
            case 'C':
                parity_sum[pos % 2]++;
                r++;
                break;
            case 'Z':
                parity_sum[pos % 2]--;
                g++;
                break;
        }
        cout << calc_res(r, g, b) << "\n";
    }
}