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

const int N = 2e5 + 5;
const long long mod = 1e9 + 7;
const long long inv3 = 333333336;

int n, q;
string S;
int trnsl[256];
int kub[3];
int przep[2];
long long pot[N]; // power of 2

// 2 cos(npi / 3)
int twice_cos(int k) {
	return round(2. * cos((double) k * M_PI / 3.));
}

long long binom_sum(int k, int s) {
	return (pot[k] + twice_cos(k - 2 * s) + mod) * inv3 % mod;
}

long long solve() {
	if (n == 1) {
		return (S[0] == 'N') ? 2 : 1;
	}

	int curr_sum = kub[0] - kub[1];
	int s = abs((-2) * (curr_sum + kub[2])) % 3;
	if (curr_sum + kub[2] < 0 && s != 0) {
		s = 3 - s;
	}
	int minus = (przep[0] == 0) + (przep[1] == 0);
	if (n % 2 == 0) {
		minus = 0;
	}
	return (pot[kub[2]] - binom_sum(kub[2], s) - minus + 2 * mod) % mod;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> q;
	cin >> S;
	trnsl['Z'] = 1;
	trnsl['N'] = 2;
	for (int i = 0; i < n; i++) {
		kub[trnsl[S[i]]]++;
		if (trnsl[S[i]] != 2) {
			przep[trnsl[S[i]] ^ (i & 1)]++;
		}
	}
	pot[0] = 1;
	for (int i = 1; i <= n; i++) {
		pot[i] = pot[i - 1] * 2 % mod;
	}
	cout << solve() << "\n";
	while (q--) {
		int a;
		char c;
		cin >> a >> c;
		kub[trnsl[S[a - 1]]]--;
		if (trnsl[S[a - 1]] != 2) {
			przep[trnsl[S[a - 1]] ^ ((a - 1) & 1)]--;
		}
		S[a - 1] = c;
		if (trnsl[S[a - 1]] != 2) {
			przep[trnsl[S[a - 1]] ^ ((a - 1) & 1)]++;
		}
		kub[trnsl[S[a - 1]]]++;
		cout << solve() << "\n";
	}

	// cout << "siema " << binom_sum(2, 0) << endl;
}


// (11) -> (2) ok
// (111) -> (12) nie ok
// (1111) -> (22) -> (1) ok 
// (11111) -> (122) -> (11) -> (2) ok
// (111111) -> (1122)
// + 3 modulo
// da sie jesli roznica mod 3 != 0

// potrzebuje: info ile mam kazdego koloru
//             choose(n, r) + choose(n, 3 + r) + choose(n, 6 + r) + ...