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
#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
#pragma GCC diagnostic ignored "-Wclass-memaccess"
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 << ")"; }
auto operator<<(auto &o, auto a) -> decltype(all(a), o);
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x)
#else
#define deb(...) 0
#endif

const int P = 998244353, phiP = P - 1;
struct mint {
    int x = 0;
    mint operator+(mint o) const {return {x + o.x >= P ? x + o.x - P : x + o.x}; }
    mint operator-(mint o) const {return {x < o.x ? x - o.x + P : x - o.x}; }
    mint operator+() const {return *this; }
    mint operator-() const {return {x ? P - x : 0}; }
    mint operator*(mint o) const {return {int(ll(x) * o.x % P)}; }
    mint operator/(mint o) const {return *this * o.inv(); }
    mint & operator+=(mint o) {return *this = *this + o; }
    mint & operator-=(mint o) {return *this = *this - o; }
    mint & operator*=(mint o) {return *this = *this * o; }
    mint & operator/=(mint o) {return *this = *this / o; }
    mint pow(ll e) const {
        mint ret{1}, b(*this);
        for (; e; e >>= 1) {
            if (e & 1)
                ret *= b;
            b *= b;
        }
        return ret;
    }
    mint inv() const {return pow(phiP - 1); }

    static mint fromLL(ll x) {
        x %= P;
        if (x < 0) x += P;
        return mint{(int)x};
    }
};

auto &operator<<(auto &os, mint m) {
    return os << m.x;
}

template <int n>
using matrice = array<array<mint, n>, n>;

void mulAdd(ll &ref, mint a, mint b) {
    if (ref < 0)
        ref += ll(a.x) * b.x;
    else
        ref += ll(a.x) * (b.x - P);
}

template <int n>
void matMul(const matrice<n> &x, const matrice<n> &y, matrice<n> &out) {
    // memset(&out, 0, sizeof out);
    array<array<ll, n>, n> tmp{};

    rep(i, n) rep(j, n) rep(k, n)
        mulAdd(tmp[i][k], x[i][j], y[j][k]);
        // out[i][k] += x[i][j] * y[j][k];
    rep(i, n) rep(j, n)
        out[i][j] = mint::fromLL(tmp[i][j]);
}

const int A = 6;

void connectMatrice(const array<int, A> &revord_a, const array<int, A> &ord_b,
                    const matrice<A> &mat_a, const matrice<A> &mat_b, matrice<A> &mat) {

    // memset(&mat, 0, sizeof mat);
    array<array<ll, A>, A> tmp{};

    rep(i, A) rep(j, A) {
        if (ord_b[j] == -1) {
            tmp[i][j] += mat_a[i][j].x;
            // mat[i][j] += mat_a[i][j];
        }
        if (revord_a[i] == -1) {
            tmp[i][j] += mat_b[i][j].x;
            // mat[i][j] += mat_b[i][j];
        }
    }

    rep(j, A) rep(k, A) rep(i, A) rep(l, A) {
        if (ord_b[j] != -1 && ord_b[j] < ord_b[k])
            continue;
        if (revord_a[k] != -1 && revord_a[k] < revord_a[j])
            continue;

        mulAdd(tmp[i][l], mat_a[i][j], mat_b[k][l]);
        // mat[i][l] += mat_a[i][j] * mat_b[k][l];
    }

    rep(i, A) rep(j, A)
        mat[i][j] = mint::fromLL(tmp[i][j]);
}

void forLetter(char ch, array<int, A> &ord, array<int, A> &revord, matrice<A> &single, matrice<A + 1> &total) {
    memset(&ord, -1, sizeof ord);
    memset(&revord, -1, sizeof revord);
    memset(&single, 0, sizeof single);
    memset(&total, 0, sizeof total);

    int id = ch - 'a';
    ord[id] = revord[id] = 0;
    single[id][id].x = 1;
    rep(i, sz(total)) {
        total[i][i].x = 1;
        total[id][i].x = 1;
    }
}

void connectOrders(const array<int, A> &x, const array<int, A> &y, array<int, A> &ret) {
    array<int, A> when{};
    memset(&when, -1, sizeof when);

    int nextInX = A;
    ret = x;

    rep(i, A) {
        if (x[i] != -1) continue;
        nextInX--;
        if (y[i] == -1) continue;
        when[y[i]] = i;
    }
    for (int i : when) {
        if (i == -1) continue;
        ret[i] = nextInX++;
    }
}

struct DS {
    int n = 1;
    vector<array<int, A>> ords, revords;
    vector<matrice<A>> singles;
    vector<matrice<A + 1>> totals;

    DS(const string &s) {
        while (n < sz(s)) n *= 2;
        ords.resize(2 * n);
        revords.resize(2 * n);
        singles.resize(2 * n);
        totals.resize(2 * n);
        n = sz(s);
        init(s, 1, 0, n - 1);
    }

    void pull(int node) {
        connectOrders(ords[node * 2], ords[node * 2 + 1], ords[node]);
        connectOrders(revords[node * 2 + 1], revords[node * 2], revords[node]);
        connectMatrice(revords[node * 2], ords[node * 2 + 1], singles[node * 2], singles[node * 2 + 1], singles[node]);
        matMul<A + 1>(totals[2 * node + 1], totals[2 * node], totals[node]);
    }

    void init(const string &s, int node, int nl, int nr) {
        if (nl == nr) {
            forLetter(s[nl], ords[node], revords[node], singles[node], totals[node]);
            return;
        }
        int mid = (nl + nr) / 2;
        init(s, node * 2, nl, mid);
        init(s, node * 2 + 1, mid + 1, nr);
        pull(node);
    }

    void setLetter(int pos, char ch, int node = 1, int nl = 0, int nr = -1) {
        if (nr == -1) nr = n - 1;

        if (nl == nr) {
            assert(pos == nl);
            forLetter(ch, ords[node], revords[node], singles[node], totals[node]);
            return;
        }

        int mid = (nl + nr) / 2;
        if (pos <= mid) setLetter(pos, ch, node * 2, nl, mid);
        else setLetter(pos, ch, node * 2 + 1, mid + 1, nr);
        pull(node);
    }

    mint countSingles() {
        mint ans;
        for (auto &ref : singles[1]) for (mint m : ref)
            ans += m;
        return ans;
    }

    mint countAll() {
        mint ans;
        rep(i, A)
            ans += totals[1][i][A];
        return ans;
    }
};

pii brute(string word) {
    int countSingles = 0, countAll = 0;
    map<string, int> mapka;
    fwd(msk, 1, 1 << sz(word)) {
        string here;
        rep(i, sz(word)) if (msk & (1 << i))
            here.pb(word[i]);
        mapka[here]++;
    }
    countAll = sz(mapka);
    for (auto &[_, c] : mapka)
        countSingles += c == 1;
    return {countAll, countSingles};
}

void solve() {
    int n, q;
    string s;
    cin >> n >> q >> s;

    DS ds(s);

    auto query = [&]() {
        mint alls = ds.countAll();
        mint singles = ds.countSingles();
        mint ans = alls - singles;
        cout << ans.x << '\n';

        #ifdef LOC
        // if (n <= 15) {
        //     auto [brAll, brSingle] = brute(s);
        //     if (brSingle != singles.x || brAll != alls.x) {
        //         deb(brAll, brSingle, alls, singles);
        //         exit(-1);
        //     }
        // }
        #endif
    };

    query();
    rep(_, q) {
        int pos;
        char ch;
        cin >> pos >> ch;
        --pos;
        s[pos] = ch;
        ds.setLetter(pos, ch);
        query();
    }
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    cout << fixed << setprecision(10);


    // mt19937_64 rnd(chrono::high_resolution_clock().now().time_since_epoch().count());
    // const int n = 75000, q = 75000;
    // string s(n, 'a');
    // for (char &ch : s) ch = char('a' + rnd() % A);
    // DS ds(s);
    // rep(_, q)
    //     ds.setLetter(rnd() % n, char('a' + rnd() % A));

    solve();
    cout << flush;
    _Exit(0);
}