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
#include <cstdio>

#define scanf(...) scanf(__VA_ARGS__)?:0
#define MOD 1000000007

typedef long long ll;

struct Mod {
	int val;
	Mod() : val(0) {}
	Mod(int a) : val(a) {}
	Mod& operator+=(const Mod &other) {
		int x = val + other.val;
		val = (x >= MOD) ? x - MOD : x;
		return *this;
	}
	Mod& operator *= (const Mod &other) {
		val = (ll) val * other.val % MOD;
		return *this;
	}
};

Mod operator+(Mod a, Mod b) {
	int x = a.val + b.val;
	return (x >= MOD) ? x - MOD : x;
}

Mod operator-(Mod a, Mod b) {
	int x = a.val - b.val + MOD;
	return (x >= MOD) ? x - MOD : x;
}

Mod operator*(Mod a, Mod b) {
	return (ll)a.val * b.val % MOD;
}

Mod pow(Mod a, int b) {
	Mod x = 1;
	while (b) {
		if (b & 1) x *= a;
		a *= a;
		b >>= 1;
	}
	return x;
}

Mod inv(Mod a) {
	return pow(a, MOD-2);
}

Mod operator/(Mod a, Mod b) {
	return a * inv(b);
}

int n, q, cz, in, b1, b2;
char s[200001];

struct vec {
	Mod arr[3];
};

struct Matrix {
	Mod arr[3][3];
	Matrix operator* (const Matrix &other) {
		Matrix r;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				for (int k = 0; k < 3; k++) r.arr[i][j] += arr[i][k] * other.arr[k][j];
			}
		}
		return r;
	}
};

Matrix pow(Matrix a, int b) {
	Matrix r;
	for (int i = 0; i < 3; i++) r.arr[i][i] = 1;
	
	while (b) {
		if (b&1) r = r * a;
		a = a * a;
		b >>= 1;
	}
	
	return r;
}

vec operator*(const Matrix &u, const vec &v) {
	vec r;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			r.arr[i] += u.arr[i][j] * v.arr[j];
		}
	}
	return r;
}

Mod result(int cz, int ilen) {
	cz = (cz % 3 + 3) % 3;
	vec v;
	v.arr[cz] = 1;
	Matrix m;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (i != j) m.arr[i][j] = 1;
		}
	}
	
	v = pow(m, ilen) * v;
	return v.arr[1] + v.arr[2];
}

int main() {
	scanf("%d%d", &n, &q);
	scanf(" %s", s+1);
	for (int i = 1; i <= n; i++) {
		switch (s[i]) {
			case 'C' :
				cz++;
				if (i % 2 == 0) b1++;
				else b2++;
				break;
			case 'Z' :
				cz--;
				if (i % 2 == 0) b2++;
				else b1++;
				break;
			case 'N' : in++; break;
		}
	}
	
	Mod r = result(cz, in);
	if (n % 2 != 0 && n > 1) {
		if (b1 == 0) r = r-1;
		if (b2 == 0) r = r-1;
	}
	
	printf("%d\n", r.val);
	
	while (q--) {
		int pos; char c;
		scanf("%d %c", &pos, &c);
		switch (s[pos]) {
			case 'C':
				cz--;
				if (pos % 2 == 0) b1--;
				else b2--;
				break;
			case 'Z': cz++;
				if (pos % 2 == 0) b2--;
				else b1--;
				break;
			case 'N': in--; break;
		}
		
		s[pos] = c;
		switch (s[pos]) {
			case 'C':
				cz++;
				if (pos % 2 == 0) b1++;
				else b2++;
				break;
			case 'Z':
				cz--;
				if (pos % 2 == 0) b2++;
				else b1++;
				break;
			case 'N': in++; break;
		}
		
		Mod r = result(cz, in);
		if (n % 2 != 0 && n > 1) {
			if (b1 == 0) r = r-1;
			if (b2 == 0) r = r-1;
		}
		
		printf("%d\n", r.val);
	}
}