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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//Andrzej Jabłoński
#include "bits/stdc++.h"
#define forx(i,b,e) for(std::common_type<decltype(b, e)>::type i = (b), ie = (e); i < ie; i++)
#define pow2(x) (1ll<<(x))
#define all(x) begin(x), end(x)
#define until(x) while(!(x))
#define len(x) (int)size(x)

using namespace std;
using llong = long long;

template<class T> void __echo(const T& x, string sep) { cerr << x << sep; }
void __echo(const string& x, string sep) { cerr << "\"" << x << "\"" << sep; }
void __echo(const char& x, string sep = ", ") { cerr << "\'" << x << "\'" << sep; }
template<class T>
void __echo(const vector<T>& x, string sep) {
    cerr << "{";
    for (size_t i = 0; i < x.size(); i++)
        __echo(x[i], i + 1 == x.size() ? "" : ", ");
    cerr << "}" << sep;
}

template<class T1, class T2>
void __echo(const pair<T1, T2>& x, string sep = ", ")
{
    cerr << "("; __echo(x.first, ", "); __echo(x.second, ""); cerr << ")" << sep;
}

void __echo(istringstream& stream) {
    cerr << "\n";
}
template<size_t> struct Int {};
template<class Tuple, size_t Pos> void print_tuple(const Tuple& x, Int<Pos>) {
    __echo(get<tuple_size<Tuple>::value - Pos>(x), ", ");
    print_tuple(x, Int<Pos - 1>());
}
template<class Tuple>
void print_tuple(const Tuple& x, Int<1>) { cerr << get<tuple_size<Tuple>::value - 1>(x); }
template<class... Args>
void print_tuple(const tuple<Args...>& x) { print_tuple(x, Int<sizeof... (Args)>()); }
template<class... Args>
void __echo(const tuple<Args...>& x, string sep) { cerr << "("; print_tuple(x); cerr << ")" << sep; }
template<class T>
bool _char_const(const T& x) { return false; }
bool _char_const(const char* x) { cerr << x; return true; }
template<class T>
bool isCharConst(const T& x) { return false; }
bool isCharConst(const char* x) { return true; }
string trim(string x) {
    return string(begin(x) + x.find_first_not_of(' '), begin(x) + x.find_last_not_of(' ') + 1);
}
template<class T, class... Args>
void __echo(istringstream& stream, const T& x, Args... args) {
    static int cnt[200] = { 0 };//no bo po co to tworzyc od nowa?
    string name;
    do {
        string s;
        getline(stream, s, ',');
        for (auto c : s)
            cnt[(int)c]++;
        name += s + ',';
    } while (not(cnt['('] == cnt[')']
        &&
        cnt['['] == cnt[']']
        &&
        cnt['{'] == cnt['}']
        &&
        cnt['\"'] % 2 == 0
        &&
        cnt['\''] % 2 == 0));
    name.pop_back();//usuwamy ','

    if (!_char_const(x)) {
        cerr << trim(name) << " = ";
        __echo(x, "; ");
    }

    __echo(stream, args...);
}
#define echo(...) if(DBG) {istringstream str(#__VA_ARGS__); __echo(str, __VA_ARGS__); }
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

#define TESTSETS 0
#define DBG 0
constexpr int mod = int(1000'000'000 + 7);

map<string, bool> ok;
bool test(const string& s) {
    auto it = ok.find(s);
    if (it != ok.end()) return it->second;

    forx(i, 0, size(s) - 1) {
        if (s[i] == s[i + 1]) {
            string t = s.substr(0, i) + (s[i] == 'C' ? 'Z' : 'C') + s.substr(i + 2);
            if (test(t)) {
                ok[s] = true;
                return true;
            }
        }
    }

    ok[s] = false;
    return false;
}

int rnd(int a, int b) {
    return rand() % (b - a + 1) + a;
}

void brut_gen()
{
    ok["C"] = ok["Z"] = true;

    int a = 3, b = 8;

    int n = 1;
    while (n < 20) {
        string s = string(n, '#');
        forx(msk, 0, pow2(n)) {
            forx(b, 0, n) {
                s[b] = "CZ"[(msk & pow2(b)) == 0];
            }
            int c = (int)count(all(s), 'C');
            int z = n - c;
            bool git = test(s);
            if ((c - z) % 3 != 0 && !git)
                cout << s << ": " << git << "\n";
        }
        n++;
    }
}

void solve_n_1(int n, int q, string s)
{
    cout << 1 + (s[0] == 'N') << "\n";
    while (q--) {
        int k; cin >> k >> s[0];
        cout << 1 + (s[0] == 'N') << "\n";
    }
}

void solve() {
    int n, q; string s;
    cin >> n >> q >> s;

    if (n == 1) {
        solve_n_1(n, q, s);
        return;
    }

    int cCZ = 0; //liczba dobrze ustawionych pozycji do ciagu CZCZCZCZC
    int cZC = 0;
    int free = 0; //liczba wolnych pozycji, czyli liczba czastek N
    int cntC = 0;
    int cntZ = 0;

    auto ok_CZ = [&](int idx, char value)->bool {
        if (idx % 2 == 0 && value != 'Z') return true;
        if (idx % 2 == 1 && value != 'C') return true;
        return false;
    };

    auto ok_ZC = [&](int idx, char value)->bool {
        if (idx % 2 == 0 && value != 'C') return true;
        if (idx % 2 == 1 && value != 'Z') return true;
        return false;
    };

    auto update_s = [&](int i, char c) {
        if (s[i] == c)
            return;

        // Na pozycje i wpisz znak c 
        // i zaktualizuj info o CZCZC... i ZCZCZ...

        if (ok_CZ(i, s[i]) && !ok_CZ(i, c))
            cCZ--;
        if (!ok_CZ(i, s[i]) && ok_CZ(i, c))
            cCZ++;

        if (ok_ZC(i, s[i]) && !ok_ZC(i, c))
            cZC--;
        if (!ok_ZC(i, s[i]) && ok_ZC(i, c))
            cZC++;

        // oraz o ilosci N (free), Z (cntZ), C (cntC)
        if (s[i] == 'N')
            free--;
        if (s[i] == 'Z')
            cntZ--;
        if (s[i] == 'C')
            cntC--;

        if (c == 'N')
            free++;
        if (c == 'Z')
            cntZ++;
        if (c == 'C')
            cntC++;

        s[i] = c;
    };

    forx(i, 0, n) {
        if (s[i] == 'N')
            free++;
        if (s[i] == 'Z')
            cntZ++;
        if (s[i] == 'C')
            cntC++;

        if (ok_CZ(i, s[i]))
            cCZ++;
        if (ok_ZC(i, s[i]))
            cZC++;
    }

    auto seqCZ = [&]() { return cCZ == n; };//czy da sie stworzyc ciag CZC...
    auto seqZC = [&]() { return cZC == n; };

    // ------------- preprocessing [3][3] ---------------
    vector<int> p2mod(n + 42, 1);
    forx(i, 1, n + 42) p2mod[i] = (p2mod[i - 1] * 2ll) % mod;

    constexpr int inv3 = 333333336;
    constexpr int inv2 = 500000004;

    auto n1p = [&](int n) {
        return (n % 2 ? mod - 1 : 1); //(-1)^n modulo
    };

    auto mmul = [&](int a, int b)->int {
        a %= mod; b %= mod;
        llong res = (((llong)a * b) % mod + mod) % mod;
        return res % mod;
    };

    auto madd = [&](int a, int b)->int {
        a %= mod; b %= mod;
        return (((llong)a + b) % mod + mod) % mod;
    };

    auto seq = [&](int N, int r) {
        int n = N / 3; //podloga
        int magic = 0;

        if (N % 3 == 0) {
            if (r % 3 == 0) {
                magic = mmul(inv3, madd(mmul(2, n1p(n)), p2mod[3 * n]));
            }
            else if (r % 3 == 1) {
                magic = mmul(inv3, madd(n1p(n + 1), p2mod[3 * n]));
            }
            else {
                magic = mmul(inv3, madd(n1p(n + 1), p2mod[3 * n]));
            }
        }
        else if (N % 3 == 1) {
            if (r % 3 == 0) {
                magic = mmul(inv3, madd(n1p(n), p2mod[3 * n + 1]));
            }
            else if (r % 3 == 1) {
                magic = mmul(inv3, madd(n1p(n), p2mod[3 * n + 1]));
            }
            else {
                magic = mmul(mmul(-2, inv3), madd(n1p(n), -p2mod[3 * n]));
            }
        }
        else {
            if (r % 3 == 0) {
                magic = mmul(inv3, madd(n1p(n + 1), p2mod[3 * n + 2]));
            }
            else if (r % 3 == 1) {
                magic = mmul(mmul(2, inv3), madd(n1p(n), p2mod[3 * n + 1]));
            }
            else {
                magic = mmul(inv3, madd(n1p(n + 1), p2mod[3 * n + 2]));
            }
        }
        if (DBG) echo(N, r, p2mod[N], magic);

        return madd(p2mod[N], -magic);
    };

    auto good_seq = [&]()->int {
        if (free == 0) {
            // odpowiedz to zawsze 1 lub 0
            return ((cntC - cntZ) % 3 != 0) && !seqCZ() && !seqZC();
        }
        else {
            int bad_mod = (2 * (cntZ - cntC + free) % 3 + 3) % 3;// r (bad_mod) 
            int special_combinations = (n % 2 == 1 ? seqCZ() + seqZC() : 0); //moze byc jednak dwie na raz (NNNN)
            int good_mod_combinations = seq(free, bad_mod);
            int ans = madd(good_mod_combinations, -special_combinations);
            return ans;
        }
    };



    // -------------------- odpowiedzi na zapytania ---------------------
    int ans0 = good_seq();
    cout << ans0 << "\n";

    forx(query, 0, q) {
        int idx; char c; cin >> idx >> c; idx -= 1;
        update_s(idx, c);
        int ans = good_seq();
        cout << ans << "\n";
    }
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    if (TESTSETS)
        cin >> t;

    while (t--)
        solve();
}