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

#define rep(i, b, e) for(int i = (b); i <= (e); i++)
#define per(i, b, e) for(int i = (e); i >= (b); i--)
#define FOR(i, b, e) rep(i, b, (e) - 1)
#define SZ(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

auto &operator<<(auto &o, pair<auto, auto> p) {
    return o << "(" << p.st << ", " << p.nd << ")"; }
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
    o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
    return o << "}"; }
#ifdef LOCAL
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \
    ((cerr << $ << "; "),...) << endl; }(x)
#else
#define deb(...)
#endif

const int mod = 998'244'353;
const int A = 6;
const int AL = A + 1;

template<size_t N>
array<int, N> operator+(const array<int, N> &a, const array<int, N> &b) {
    array<int, N> res{};
    for(size_t i = 0; i < N; i++) {
        res[i] = a[i] + b[i];
        res[i] -= (res[i] >= mod) * mod;
    }
    return res;
}

template<size_t N>
array<int, N> operator-(const array<int, N> &a, const array<int, N> &b) {
    array<int, N> res{};
    for(size_t i = 0; i < N; i++) {
        res[i] = a[i] - b[i];
        res[i] += (res[i] < 0) * mod;
    }
    return res;
}

template<size_t N>
int operator*(const array<int, N> &a, const array<int, N> &b) {
    int res = 0;
    for(size_t i = 0; i < N; i++) res = (res + ll(a[i]) * b[i]) % mod;
    return res;
}

struct Kubel {
    string s;
    int sh, n;
    array<int, AL> statAll[AL];
    array<int, AL> statUni[AL];
    int first[AL], last[AL];

    Kubel(string _s, int _sh) : s(_s), sh(_sh), n(SZ(s)) { rec(); }

    void update(int pos, char c) { s[pos] = c; rec(); }

    void rec() {
        FOR(i, 0, AL) FOR(j, 0, AL) statAll[i][j] = i == j;
        FOR(i, 0, AL) FOR(j, 0, AL) statUni[i][j] = i == j;
        FOR(i, 0, AL) first[i] = last[i] = -1;
        FOR(i, 0, n) {
            int me = s[i] - 'a';
            // compAll
            auto oldRes = statAll[A];
            statAll[A] = statAll[A] + statAll[A] - statAll[me];
            statAll[me] = oldRes;
            // compMe
            array<int, AL> resUni{};
            if(first[me] == -1) {
                first[me] = i + sh;
                resUni[me] = 1;
            }
            FOR(j, 0, AL) if(last[j] != -1 && last[me] <= last[j]) resUni = resUni + statUni[j];
            statUni[me] = resUni;
            last[me] = i + sh;
        }
    }

    void compAll(array<int, AL> &akt) {
        array<int, AL> res;
        FOR(i, 0, AL) res[i] = akt * statAll[i];
        akt = res;
    }

    void compUni(array<int, AL> &pos, array<int, AL> &akt) {
        array<int, AL> start{};
        FOR(i, 0, AL) {
            if(first[i] == -1) start[i] = akt[i];
            else {
                FOR(j, 0, AL) {
                    if(pos[j] != -2 && pos[i] <= pos[j] && (first[j] == -1 || first[i] <= first[j])) {
                        start[i] = (start[i] + akt[j]) % mod;
                    }
                }
            }
        }
        array<int, AL> res;
        FOR(i, 0, AL) res[i] = start * statUni[i];
        FOR(i, 0, AL) if(last[i] != -1) pos[i] = last[i];
        akt = res;
    }
};

const int B = 500;

void solve() {
    int n, q;
    string s;
    cin >> n >> q >> s;
    vector<Kubel> kubly;
    for(int i = 0; i < n; i += B) {
        kubly.pb(Kubel(s.substr(i, min(B, n - i)), i));
    }
    auto getAns = [&]() {
        array<int, AL> aktAll{}, pos{}, aktUni{};
        aktAll[A] = 1;
        FOR(i, 0, A) pos[i] = -2;
        pos[A] = -1;
        aktUni[A] = 1;
        for(auto &x: kubly) {
            x.compAll(aktAll);
            x.compUni(pos, aktUni);
        }
        int sAll = aktAll[A];
        int sUni = 0;
        FOR(i, 0, AL) sUni = (sUni + aktUni[i]) % mod;
        return (sAll - sUni + mod) % mod;
    };
    cout << getAns() << '\n';
    FOR(i, 0, q) {
        int p; char c;
        cin >> p >> c;
        p--;
        kubly[p / B].update(p % B, c);
        cout << getAns() << '\n';
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int tt = 1;
    // cin >> tt;
    FOR(te, 0, tt) solve();
    return 0;
}