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
// #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <iostream>
#include <vector>
using namespace std;

const char R_CHAR = 'C';
const char G_CHAR = 'Z';
const char B_CHAR = 'N';

class int2137
{
private:
    int64_t inner;
    static const int64_t MOD_BASE = 1000000007;

public:
    int2137(int64_t x = 0)
    {
        inner = x;
    }

    int2137 operator+(int2137 const &obj)
    {
        int2137 res;
        res.inner = (inner + obj.inner) % MOD_BASE;
        return res;
    }

    int2137 operator-(int num)
    {
        int2137 res;
        res.inner = (inner - num) % MOD_BASE;
        if (res.inner < 0)
        {
            res.inner += MOD_BASE;
        }
        return res;
    }

    int2137 &operator+=(int num)
    {
        inner = (inner + num) % MOD_BASE;
        return *this;
    }
    int2137 &operator-=(int num)
    {
        inner = inner - num;
        if (inner < 0)
        {
            inner += MOD_BASE;
        }
        return *this;
    }
    int2137 &operator+=(int64_t num)
    {
        inner = (inner + num) % MOD_BASE;
        return *this;
    }
    int2137 &operator++()
    {
        inner++;
        inner %= MOD_BASE;
        return *this;
    }

    int2137 &operator--()
    {
        inner--;
        if (inner == -1)
        {
            inner += MOD_BASE;
        }
        return *this;
    }

    int2137 &operator+=(int2137 const &obj)
    {
        inner = (inner + obj.inner) % MOD_BASE;
        return *this;
    }

    friend bool operator==(const int2137 &lhs, const int2137 &rhs)
    {
        return (lhs.inner == rhs.inner);
    }

    int2137 &
    operator*=(int num)
    {
        inner = (inner * num) % MOD_BASE;
        return *this;
    }
    int2137 operator*(int2137 const &obj)
    {
        int2137 res;
        res.inner = (inner * obj.inner) % MOD_BASE;
        return res;
    }

    friend ostream &operator<<(ostream &output, const int2137 &num)
    {
        output << num.inner;
        return output;
    }
};

bool is_breakable(string particles)
{
    int i = 0;
    for (; i < particles.size() && particles[i] == B_CHAR; i++)
        continue;

    char should_be = particles[i];
    for (; i < particles.size(); i++)
    {
        switch (should_be)
        {
        case R_CHAR:
            if (particles[i] == G_CHAR)
                return false;

            should_be = G_CHAR;
            break;

        case G_CHAR:
            if (particles[i] == R_CHAR)
                return false;

            should_be = R_CHAR;
            break;
        }
    }
    return true;
}

vector<vector<int64_t>> bv;
void enlarge_vec(int n);
int64_t binomial_coefficents(int n, int k);
void enlarge_vec(int n)
{

    int old_size = bv.size();
    for (int i = old_size; i <= n; i++)
    {
        vector<int64_t> nv((i / 2) + 1);
        nv[0] = 1;
        for (int j = 1; j < nv.size(); j++)
        {
            nv[j] = (binomial_coefficents(i - 1, j - 1) + binomial_coefficents(i - 1, j)) % 1000000007;
        }
        bv.push_back(nv);
    }
}

int64_t binomial_coefficents(int n, int k)
{
    if (k == 0 || k == n)
    {
        return 1;
    }
    if (n >= bv.size())
    {
        enlarge_vec(n);
    }

    if (k < n - k)
    {
        return bv[n][k];
    }
    return bv[n][n - k];
}

int2137 solveB(string particles, int r, int g, int b)
{
    int2137 total = 0;
    int base = r + g * 2;
    for (int additional_r = 0; additional_r <= b; additional_r++)
    {
        int additional_g = (b - additional_r);

        if ((base + additional_r + 2 * additional_g) % 3 == 0)
        {
            continue;
        }
        total += binomial_coefficents(b, additional_r);
    }
    return total;
}

int2137 solve(string particles, int r, int g, int b)
{
    int2137 total = 1;
    if (b != 0)
    {
        total = solveB(particles, r, g, b);
    }
    if (b == particles.size() && particles.size() != 1)
    {
        --total;
        --total;
    }
    else if (abs(r - g) <= b && particles.size() != 1 && is_breakable(particles))
    {
        --total;
    }

    return total;
}

int main()
{
    FAST int n, q;
    cin >> n >> q;
    int r = 0;
    int g = 0;
    int b = 0;
    bv.reserve(n);
    bv = {
        {1},
        {1},
        {1, 2},
        {1, 3},
        {1, 4, 6},
        {1, 5, 10},
        {1, 6, 15, 20},
        {1, 7, 21, 35},
        {1, 8, 28, 56, 70},
        {1, 9, 36, 84, 126},
        {1, 10, 45, 120, 210, 252},
        {1, 11, 55, 165, 330, 462},
        {1, 12, 66, 220, 495, 792, 924},
        {1, 13, 78, 286, 715, 1287, 1716},
        {1, 14, 91, 364, 1001, 2002, 3003, 3432},
        {1, 15, 105, 455, 1365, 3003, 5005, 6435},
        {1, 16, 120, 560, 1820, 4368, 8008, 11440, 12870},
        {1, 17, 136, 680, 2380, 6188, 12376, 19448, 24310},
        {1, 18, 153, 816, 3060, 8568, 18564, 31824, 43758, 48620},
        {1, 19, 171, 969, 3876, 11628, 27132, 50388, 75582, 92378},
        {1, 20, 190, 1140, 4845, 15504, 38760, 77520, 125970, 167960, 184756},
        {1, 21, 210, 1330, 5985, 20349, 54264, 116280, 203490, 293930, 352716},
        {1, 22, 231, 1540, 7315, 26334, 74613, 170544, 319770, 497420, 646646, 705432},
        {1, 23, 253, 1771, 8855, 33649, 100947, 245157, 490314, 817190, 1144066, 1352078},
        {1, 24, 276, 2024, 10626, 42504, 134596, 346104, 735471, 1307504, 1961256, 2496144, 2704156},
        {1, 25, 300, 2300, 12650, 53130, 177100, 480700, 1081575, 2042975, 3268760, 4457400, 5200300},
    };
    string particles;
    cin >> particles;
    for (int i = 0; i < n; i++)
    {
        switch (particles[i])
        {
        case R_CHAR:
            r++;
            break;
        case G_CHAR:
            g++;
            break;
        case B_CHAR:
            b++;
            break;
        }
    }

    int2137 ret = solve(particles, r, g, b);
    cout << ret << endl;

    int idx;
    char newParticle;
    for (int i = 0; i < q; i++)
    {
        cin >> idx >> newParticle;
        // unify idx to array
        idx--;
        char oldParticle = particles[idx];
        particles[idx] = newParticle;
        if (oldParticle == newParticle)
        {
            cout << ret << endl;
            continue;
        }
        switch (oldParticle)
        {
        case R_CHAR:
            r--;
            break;
        case G_CHAR:
            g--;
            break;
        case B_CHAR:
            b--;
            break;
        }

        switch (newParticle)
        {
        case R_CHAR:
            r++;
            break;
        case G_CHAR:
            g++;
            break;
        case B_CHAR:
            b++;
            break;
        }
        ret = solve(particles, r, g, b);
        cout << ret << endl;
    }
}
//fixme plz XD