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
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n)    fwd(i, 0, n)
#define all(X)       X.begin(), X.end()
#define sz(X)        int(size(X))
#define pb           push_back
#define eb           emplace_back
#define st           first
#define nd           second
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) {
	*(int *)0 = 0;
});
#	define DTP(x, y)                                      \
		auto operator<<(auto &o, auto a)->decltype(y, o) { \
			o << "(";                                      \
			x;                                             \
			return o << ")";                               \
		}
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) {
	((cerr << x << ", "), ...) << '\n';
}
#	define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#	define deb(...) 0
#endif

constexpr int mod = 998244353;
constexpr int alf = int('f' - 'a') + 1;
constexpr int alf2 = alf + 1;

typedef array<int, alf> vec_t;
typedef array<vec_t, alf> mat_t;
typedef array<int, alf2> vec2_t;
typedef array<vec2_t, alf2> mat2_t;

inline void mul(const mat_t &a, const mat_t &b, mat_t &res) {
	static ll acc[alf][alf];
	rep(i, alf) fill(acc[i], acc[i] + alf, 0);
	rep(i, alf) rep(k, alf) rep(j, alf) acc[i][j] += 1ll * a[i][k] * b[k][j];
	rep(i, alf) rep(j, alf) res[i][j] = int(acc[i][j] % mod);
}

inline void mul2(const mat2_t &a, const mat2_t &b, mat2_t &res) {
	static ll acc[alf2][alf2];
	rep(i, alf2) fill(acc[i], acc[i] + alf2, 0);
	rep(i, alf2) rep(k, alf2) rep(j, alf2) acc[i][j] += 1ll * a[i][k] * b[k][j];
	rep(i, alf2) rep(j, alf2) res[i][j] = int(acc[i][j] % mod);
}

constexpr int base = (1 << 16);

mat_t dp[2 * base];

mat2_t dp2[2 * base];

set<int> positions[alf];

vi a;

void make_matrix(mat_t &m, int p) {
	rep(i, alf) {
		fill(all(m[i]), 0);
		m[i][i] = 1;
	}
	int nxt_pos_equal = *positions[a[p]].upper_bound(p);
	rep(i, alf) {
		int nxt_pos = *positions[i].upper_bound(p);
		if (nxt_pos < nxt_pos_equal)
			m[a[p]][i] = 1;
	}
}

void make_matrix2(mat2_t &m, int p) {
	rep(i, alf2) {
		fill(all(m[i]), 0);
		m[i][i] = 1;
	}
	rep(i, alf2) m[i][a[p]] = 1;
}

void update_pos(int p) {
	for (int i = (base + p) / 2; i >= 1; i /= 2)
		mul(dp[2 * i], dp[2 * i + 1], dp[i]);
}

void update_pos2(int p) {
	for (int i = (base + p) / 2; i >= 1; i /= 2)
		mul2(dp2[2 * i], dp2[2 * i + 1], dp2[i]);
}

inline void mul_vec(vec_t &vec, const mat_t &m) {
	static ll acc[alf];
	fill(acc, acc + alf, 0);
	rep(i, alf) rep(j, alf) acc[j] += 1ll * vec[i] * m[i][j];
	rep(i, alf) vec[i] = int(acc[i] % mod);
}

inline void mul_vec2(vec2_t &vec, const mat2_t &m) {
	static ll acc[alf2];
	fill(acc, acc + alf2, 0);
	rep(i, alf2) rep(j, alf2) acc[j] += 1ll * vec[i] * m[i][j];
	rep(i, alf2) vec[i] = int(acc[i] % mod);
}

void get_prefix(vec_t &vec, int p) {
	int node = 1;
	int l = 0, r = base;
	while (l + 1 < r) {
		int m = (l + r) / 2;
		if (p < m) {
			r = m;
			node = 2 * node;
		} else {
			mul_vec(vec, dp[2 * node]);
			l = m;
			node = 2 * node + 1;
		}
	}
}

int get_all() {
	vec2_t vec;
	fill(all(vec), 0);
	vec[alf] = 1;
	mul_vec2(vec, dp2[1]);
	return int(accumulate(begin(vec), end(vec) - 1, 0LL) % mod);
}

int get_unique() {
	int res = 0;
	vec_t vec;
	rep(i, alf) {
		auto it = prev(end(positions[i]));
		if (it == begin(positions[i]))
			continue;
		int last_pos = *prev(it);
		fill(all(vec), 1);
		get_prefix(vec, last_pos);
		res = int((res + vec[i]) % mod);
	}
	return res;
}

int get_res() {
	int res = get_all() - get_unique();
	if (res < 0)
		res += mod;
	return res;
}

void sol() {
	int n, q;
	cin >> n >> q;
	string s;
	cin >> s;
	a = vi(n);
	rep(i, alf) positions[i].insert(n);
	rep(i, n) {
		a[i] = int(s[i] - 'a');
		positions[a[i]].insert(i);
	}
	rep(i, n) {
		make_matrix(dp[base + i], i);
		make_matrix2(dp2[base + i], i);
	}
	mat2_t identity2;
	rep(i, alf2) {
		fill(all(identity2[i]), 0);
		identity2[i][i] = 1;
	}
	fwd(i, n, base) dp2[base + i] = identity2;
	for (int i = base - 1; i >= 1; i--) {
		mul(dp[2 * i], dp[2 * i + 1], dp[i]);
		mul2(dp2[2 * i], dp2[2 * i + 1], dp2[i]);
	}
	cout << get_res() << '\n';
	rep(_, q) {
		int p;
		char c;
		cin >> p >> c;
		p -= 1;
		positions[a[p]].erase(p);
		a[p] = int(c - 'a');
		positions[a[p]].insert(p);
		make_matrix(dp[base + p], p);
		make_matrix2(dp2[base + p], p);
		update_pos(p);
		update_pos2(p);
		rep(i, alf) {
			auto it = positions[i].lower_bound(p);
			if (it != begin(positions[i])) {
				int prev_pos = *prev(it);
				make_matrix(dp[base + prev_pos], prev_pos);
				update_pos(prev_pos);
			}
		}
		cout << get_res() << '\n';
	}
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	cout << fixed << setprecision(10);
	sol();
#ifdef LOCF
	cout.flush();
	cerr << "- - - - - - - - -\n";
	(void)!system(
		"grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB
																		// ....kB
#endif
}