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
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
using namespace __gnu_pbds;
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937_64 mrand(1);
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

ll sum_of_binoms(int len, int r) {
	if (len % 3 == 0) {
		ll partial0 = ((powmod(2, len) + 2 * (-1 + 2 * (1 - len % 2))) % mod * powmod(3, mod - 2) % mod + mod) % mod;
		if (r == 0) return partial0;
		return ((powmod(2, len) - partial0) % mod + mod ) % mod * powmod(2, mod - 2) % mod;
	}
	ll partial12;
	if (len % 3 == 1)
		partial12 = ((powmod(2, len) + -1 + 2 * (1 - (len / 3 * 3) % 2)) % mod * powmod(3, mod - 2) % mod + mod) % mod;
	else
		partial12 = ((powmod(2, len) + -1 + 2 * ((len / 3 * 3) % 2)) % mod * powmod(3, mod - 2) % mod + mod) % mod;
	if (len % 3 == 2) {
		if (r == 1) return ((powmod(2, len) - 2 * partial12) % mod + mod) % mod;
		return partial12;
	}
	if (r == 2) return ((powmod(2, len) - 2 * partial12) % mod + mod) % mod;
	return partial12;
}

set<tuple<int, int, int>> intervals;

int base = 524288;
const int N = 3e6+5;
char s[N];

ll comb0[N], comb1[N], comb2[N];
int next_id;

void update(int nd) {
	nd /= 2;
	while (nd) {
		comb0[nd] = (comb0[nd + nd] * comb0[nd + nd + 1] % mod +
					comb1[nd + nd] * comb2[nd + nd + 1] % mod +
					comb2[nd + nd] * comb1[nd + nd + 1] % mod) % mod;
		comb1[nd] = (comb1[nd + nd] * comb0[nd + nd + 1] % mod +
					comb2[nd + nd] * comb2[nd + nd + 1] % mod +
					comb0[nd + nd] * comb1[nd + nd + 1] % mod) % mod;
		comb2[nd] = (comb2[nd + nd] * comb0[nd + nd + 1] % mod +
					comb1[nd + nd] * comb1[nd + nd + 1] % mod +
					comb0[nd + nd] * comb2[nd + nd + 1] % mod) % mod;
		nd /= 2;
	}
}

void remove_interval(tuple<int, int, int> &p) {
	intervals.erase(p);
	int id = base + get<2>(p);
	comb0[id] = 1;
	comb1[id] = 0;
	comb2[id] = 0;
	update(id);
}

void add_interval(int bg, int ed) {
	int len = ed - bg;
	intervals.insert({bg, ed, next_id});
	int id = base + next_id;

	comb0[id] = sum_of_binoms(len, (3 - len % 3) % 3);
	comb1[id] = sum_of_binoms(len, (3 - len % 3 + 1) % 3);
	comb2[id] = sum_of_binoms(len, (3 - len % 3 + 2) % 3);

	update(id);
	next_id++;
}

void build(int nd) {
	if (nd >= base + base) return;

	comb0[nd] = 1;

	build(nd + nd);
	build(nd + nd + 1);
}

ll res;

void check(int needed, int flag) {
	//cout << needed % 3 << endl;
	//cout << " " << needed << " flag " << flag << endl;
	if (flag == 1 && ((needed % 3 + 3) % 3 == 1 || (needed % 3 + 3) % 3 == 2) ||
			flag == 2 && ((needed % 3 + 3) % 3 == 1 || (needed % 3 + 3) % 3 == 0) ||
			flag == 3 && ((needed % 3 + 3) % 3 == 2 || (needed % 3 + 3) % 3 == 0))
			res--;
}

int main() {
	build(1);
	int n, q;
	scanf("%d%d", &n, &q);
	scanf("%s", s);

	int beg = 0, cntN = 0, cntC_odd = 0, cntZ_odd = 0, cntC_even = 0, cntZ_even = 0;
	rep(i, 0, n) {
		if (s[i] == 'N') {
			if (cntN == 0) beg = i;
			cntN++;
		} else {
			if (s[i] == 'C') {
				if (i & 1)
					cntC_odd++;
				else
					cntC_even++;
			} else {
				if (i & 1)
					cntZ_odd++;
				else
					cntZ_even++;
			}
			if (cntN > 0) add_interval(beg, i);
			cntN = 0;
		}
	}
	if (cntN > 0) add_interval(beg, n);

	int flag;
	if (((cntC_odd + cntC_even - cntZ_odd - cntZ_even) % 3 + 3) % 3 == 0) {
		flag = 1;
		res = (comb1[1] + comb2[1]) % mod;
	}
	else if (((cntC_odd + cntC_even - cntZ_odd - cntZ_even) % 3 + 3) % 3 == 1) {
		flag = 2;
		res = (comb1[1] + comb0[1]) % mod;
		//cout << " d " << comb0[1] << " " << comb1[1] << endl;
	}
	else {
		flag = 3;
		res = (comb2[1] + comb0[1]) % mod;
	}

	if (n != 1) {
	if (!cntC_odd && !cntZ_even) {
		if (cntC_even > cntZ_odd) {
			if (n % 2 == 0) {
				if (n - cntC_even - cntZ_odd >= cntC_even - cntZ_odd)
					check(-(cntC_even - cntZ_odd), flag);
			} else {
				if (n - cntC_even - cntZ_odd >= cntC_even - cntZ_odd - 1) 
					check(-(cntC_even - cntZ_odd - 1), flag);
			}
		} else if (cntC_even < cntZ_odd) {
			if (n % 2 == 0) {
				if (n - cntZ_odd - cntC_even >= cntZ_odd - cntC_even)
					check(cntZ_odd - cntC_even, flag);
			} else {
				if (n - cntZ_odd - cntC_even >= cntZ_odd - cntC_even + 1)
					check(cntZ_odd - cntC_even + 1, flag);
			}
		} else if (cntC_even == cntZ_odd) {
			if (n % 2 == 0) {
				check(0, flag);
			} else {
				check(1, flag);
			}
		}
	}

	if (!cntZ_odd && !cntC_even) {
		if (cntZ_even > cntC_odd) {
			if (n % 2 == 0) {
				if (n - cntZ_even - cntC_odd >= cntZ_even - cntC_odd)
					check(cntZ_even - cntC_odd, flag);
			} else {
				if (n - cntZ_even - cntC_odd >= cntZ_even - cntC_odd - 1) 
					check(cntZ_even - cntC_odd - 1, flag);
			}
		} else if (cntZ_even < cntC_odd) {
			if (n % 2 == 0) {
				if (n - cntC_odd - cntZ_even >= cntC_odd - cntZ_even)
					check(-(cntC_odd - cntZ_even), flag);
			} else {
				if (n - cntC_odd - cntZ_even >= cntC_odd - cntZ_even + 1)
					check(-(cntC_odd - cntZ_even + 1), flag);
			}
		} else if (cntZ_even == cntC_odd) {
			if (n % 2 == 0) {
				check(0, flag);
			} else {
				check(-1, flag);
			}
		}
	}
}	

	printf("%lld\n", res);

	while (q--) {
		int i;
		char c;
		scanf("%d %c", &i, &c);
		--i;
		if (i & 1) {
			if (s[i] == 'C') cntC_odd--;
			else if (s[i] == 'Z') cntZ_odd--;
			if (c == 'C') cntC_odd++;
			else if (c == 'Z') cntZ_odd++;
		} else {
			if (s[i] == 'C') cntC_even--;
			else if (s[i] == 'Z') cntZ_even--;
			if (c == 'C') cntC_even++;
			else if (c == 'Z') cntZ_even++;
		}

		if (s[i] == 'N' && (c == 'C' || c == 'Z')) {
			auto p = *(--intervals.lower_bound(make_tuple(i + 1, -10, -10)));
			remove_interval(p);
			int bg = get<0>(p), ed = get<1>(p);
			if (bg < i) {
				add_interval(bg, i);
			}
			if (i + 1 < ed)
				add_interval(i + 1, ed);
		}

		if ((s[i] == 'C' || s[i] == 'Z') && c == 'N') {
			auto it = intervals.lower_bound(make_tuple(i, -10, -10));
			tuple<int, int, int> p1 = make_tuple(-10, -10, -10), p2 = make_tuple(-10, -10, -10);
			if (it != intervals.end()) p2 = *it;
			if (it != intervals.begin()) p1 = *(--it);
			if (get<1>(p1) == i && get<0>(p2) == i + 1) {
				remove_interval(p1);
				remove_interval(p2);
				add_interval(get<0>(p1), get<1>(p2));
			} else if (get<1>(p1) == i) {
				remove_interval(p1);
				add_interval(get<0>(p1), i + 1);
			} else if (get<0>(p2) == i + 1) {
				remove_interval(p2);
				add_interval(i, get<1>(p2));
			} else add_interval(i, i + 1);
		}

		s[i] = c;

	if (((cntC_odd + cntC_even - cntZ_odd - cntZ_even) % 3 + 3) % 3 == 0) {
		flag = 1;
		res = (comb1[1] + comb2[1]) % mod;
	}
	else if (((cntC_odd + cntC_even - cntZ_odd - cntZ_even) % 3 + 3) % 3 == 1) {
		flag = 2;
		res = (comb1[1] + comb0[1]) % mod;
	}
	else {
		flag = 3;
		res = (comb2[1] + comb0[1]) % mod;
	}

	//cout << "cntC odd " << cntC_odd << endl;

	//cout << "cntZ odd " << cntZ_odd << endl;

	//cout << "cntC even " << cntC_even << endl;

	//cout << "cntZ even " << cntZ_even << endl;
	//cout << "flag " << flag << endl;
if (n != 1) {
	if (!cntC_odd && !cntZ_even) {
		if (cntC_even > cntZ_odd) {
			if (n % 2 == 0) {
				if (n - cntC_even - cntZ_odd >= cntC_even - cntZ_odd) 
					check(-(cntC_even - cntZ_odd), flag);
			} else {
				if (n - cntC_even - cntZ_odd >= cntC_even - cntZ_odd - 1) 
					check(-(cntC_even - cntZ_odd - 1), flag);
			}
		} else if (cntC_even < cntZ_odd) {
			if (n % 2 == 0) {
				if (n - cntZ_odd - cntC_even >= cntZ_odd - cntC_even)
					check(cntZ_odd - cntC_even, flag);
			} else {
				if (n - cntZ_odd - cntC_even >= cntZ_odd - cntC_even + 1)
					check(cntZ_odd - cntC_even + 1, flag);
			}
		} else if (cntC_even == cntZ_odd) {
			if (n % 2 == 0) {
				check(0, flag);
			} else {
				check(1, flag);
			}
		}
	}

	if (!cntZ_odd && !cntC_even) {
		if (cntZ_even > cntC_odd) {
			if (n % 2 == 0) {
				if (n - cntZ_even - cntC_odd >= cntZ_even - cntC_odd)
					check(cntZ_even - cntC_odd, flag);
			} else {
				if (n - cntZ_even - cntC_odd >= cntZ_even - cntC_odd - 1) 
					check(cntZ_even - cntC_odd - 1, flag);
			}
		} else if (cntZ_even < cntC_odd) {
			if (n % 2 == 0) {
				if (n - cntC_odd - cntZ_even >= cntC_odd - cntZ_even)
					check(-(cntC_odd - cntZ_even), flag);
			} else {
				if (n - cntC_odd - cntZ_even >= cntC_odd - cntZ_even + 1)
					check(-(cntC_odd - cntZ_even + 1), flag);
			}
		} else if (cntZ_even == cntC_odd) {
			if (n % 2 == 0) {
				check(0, flag);
			} else {
				check(-1, flag);
			}
		}
	}
}

	printf("%lld\n", res);
	}
}