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

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
  o << "{";
  for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
  return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
  return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif

template<int N>
struct IntSet {
	static constexpr int B = 64;
	uint64_t V[N / B + 1] = {};
	IntSet<(N < B + 1 ? 0 : N / B + 1)> up;
	bool has(int i) { return (V[i / B] >> i) & 1; }
	void add(int i) { 
		if (!V[i / B]) up.add(i / B);
		V[i / B] |= 1ull << i;
	}
	void del(int i) {
		if (!(V[i / B] &= ~(1ull << i))) up.del(i / B);
	}
	int next(int i) { // j > i such that j inside or -1
		auto x = V[i / B] >> i;
		if (x &= ~1) return i + __builtin_ctzll(x);
		return (i = up.next(i / B)) < 0 ? i :
			i * B + __builtin_ctzll(V[i]);
	}
	int prev(int i) { // j < i such that j inside or -1
		auto x = V[i / B] << (B - i - 1);
		if (x &= INT64_MAX)
			return i-__builtin_clzll(x);
		return (i = up.prev(i / B)) < 0 ? i :
			i * B + B - 1 - __builtin_clzll(V[i]);
	}
};
template<>
struct IntSet<0> {
	void add(int) {} void del(int) {}
	int next(int) { return -1; }
	int prev(int) { return -1; } };

constexpr uint32_t MOD = 998244353;

using M = array<array<uint32_t, 7>, 7>;
const M I = {{
  {1, 0, 0, 0, 0, 0, 0}, 
  {0, 1, 0, 0, 0, 0, 0}, 
  {0, 0, 1, 0, 0, 0, 0}, 
  {0, 0, 0, 1, 0, 0, 0}, 
  {0, 0, 0, 0, 1, 0, 0}, 
  {0, 0, 0, 0, 0, 1, 0}, 
  {0, 0, 0, 0, 0, 0, 1}, 
}};

// mozliwe opty mnozenia:
// splaszczenie
// zunrollowanie petli (lol)
// optymalniejsze mnozenie (nwm czy znamy optymalne)

struct Tree {
	typedef M T; 
	T f(const T& a, const T& b) {
    uint64_t x[7][7] = {};
    rep(i, 0, 7) rep(j, 0, 7) rep(k, 0, 7) x[i][k] += (uint64_t)a[i][j] * b[j][k];
    T c = {}; 
    rep(i, 0, 7) rep(j, 0, 7) c[i][j] = x[i][j] % MOD;
    return c;
  }
	vector<T> s; int n;
	Tree(int N = 0) : s(2*N, I), n(N) {}
	void update(int pos, T val) {
		for (s[pos += n] = val; pos /= 2;)
			s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
	}
	T query(int b, int e) { // query [b, e)
		T ra = I, rb = I;
		for (b += n, e += n; b < e; b /= 2, e /= 2) {
			if (b % 2) ra = f(ra, s[b++]);
			if (e % 2) rb = f(s[--e], rb);
		}
		return f(ra, rb);
	}
};

const int N = 50050;
int n, q;
string s;
M m1[6], m2[6][64];
Tree t1, t2;
IntSet<N> p[6];

uint32_t rozw() {
  uint64_t odp = 0;
  M a = t1.query(0, n);
  rep(i, 0, 6) odp += a[i][6];
  debug(odp);
  M b = t2.query(0, n);
  rep(i, 0, 6) odp += MOD - b[i][6];
  return odp % MOD;
}

void napraw(int i) {
  int c = s[i] - 'a';
  int nxt[6];
  rep(j, 0, 6) {
    int x = p[j].next(i);
    nxt[j] = x != -1 ? x : n;
  }
  int msk = 0;
  rep(j, 0, 6) if (nxt[j] < nxt[c]) msk |= 1 << j;
  if (nxt[c] != n) msk |= 1 << c;
  t2.update(i, m2[c][msk]);
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  rep(i, 0, 6) {
    m1[i] = I;
    rep(j, 0, 7) m1[i][i][j] = 1;
  }
  rep(i, 0, 6) rep(msk, 0, 64) {
    m2[i][msk] = I;
    rep(j, 0, 6) if ((msk >> j) & 1) m2[i][msk][i][j] = 1;
    if (!((msk >> i) & 1)) m2[i][msk][i][6] = 1; // +1 jezeli ost wyst
  }
  cin >> n >> q >> s;
  t1 = t2 = Tree(n);
  rep(i, 0, n) {
    t1.update(i, m1[s[i] - 'a']);
    p[s[i] - 'a'].add(i);
  }
  rep(i, 0, n) napraw(i);
  cout << rozw() << '\n';
  while (q--) {
    int i;
    char c;
    cin >> i >> c;
    i--;
    int zm[7];
    rep(j, 0, 6) {
      zm[j] = p[j].prev(i);
    }
    zm[6] = i;
    p[s[i] - 'a'].del(i);
    s[i] = c;
    p[c - 'a'].add(i);
    for (int x : zm) if (x != -1) napraw(x);
    t1.update(i, m1[c - 'a']);
    cout << rozw() << '\n';
  }
}