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
#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
const long long MD = 998244353;
template<long long MOD=MD> struct mdint {
    int d;
    mdint () {d=0;}
    mdint (long long _d) : d(_d%MOD){
        if(d<0) d+=MOD;
    };
    friend mdint& operator+=(mdint& a, const mdint& o) {
        a.d+=o.d; if(a.d>=MOD) a.d-=MOD;
        return a;
    }
    friend mdint& operator-=(mdint& a, const mdint& o) {
        a.d-=o.d; if(a.d<0) a.d+=MOD;
        return a;
    }
    friend mdint& operator*=(mdint& a, const mdint& o) {
        return a = mdint((ll)a.d*o.d);
    }
    mdint operator*(const mdint& o) const {
        mdint res = *this;
        res*=o;
        return res;
    }
    mdint operator+(const mdint& o) const {
        mdint res = *this;
        res+=o;
        return res;
    }
    mdint operator-(const mdint& o) const {
        mdint res = *this;
        res-=o;
        return res;
    }
    mdint operator^(long long b) const {
        mdint tmp = 1;
        mdint power = *this;
        while(b) {
            if(b&1) {
                tmp = tmp*power;
            }
            power = power*power;
            b/=2;
        }
        return tmp;
    }
    friend mdint operator/=(mdint& a, const mdint& o) {
        a *= (o^(MOD-2));
        return a;
    }
    mdint operator/(const mdint& o) {
        mdint res = *this;
        res/=o;
        return res;
    }
    auto operator<=>(const mdint& o) const { return d<=>o.d;}
    friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}
    friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}
};
using  mint = mdint<MD>;
const int N = 6;
// for counting unique subseqs.
struct node {
    mint x[N*N][N*N] = {};
    bool unit=0;
    int msk=0;
    node() {
        unit=1;
        // *this = node(0);
    }
    node(char c) {
        c-='a';
        msk=1<<int(c);

        /*
        all cx states: place c behind it.
        ax states: all those subsequences are changed
        */
        for(int i=0;i<N;++i) for(int j=0;j<N;++j) {
            x[i + N*c][c + N*j]=1;
        }
        // all others states which do not get disturbed by a C stay unchanged
        for(int i=0;i<N;++i) if(i!=c) for(int j=0;j<N;++j) if(j!=c) {
            x[j + N*i][j+N*i]=1;
        }

    }
    void merge(const node& o) {
        if(o.unit) return;
        if(unit) {
            *this=o;
            return;
        }
        node res = {};
        res.unit=0;
        for(int i=0;i<N*N;++i) for(int j=0;j<N*N;++j) for(int k=0;k<N*N;++k) {
            res.x[i][k]+=x[i][j]*o.x[j][k];
        }
        res.msk=msk|o.msk;
        
        *this=res;
    }
    mint ans() {
        mint res=0;
        // starting with aa, then ending up at
        for(int i=0;i<N;++i) for(int j=0;j<N;++j) {
            res+=x[i*(N+1)][j + N*(j)];
        }
        for(int j=0;j<N;++j) if(!(1<<j & msk)) {
            res-=1;
        }
        // only want non-empty?
        return res;
    }
    friend node merge(node a, const node& b) {
        a.merge(b);
        return a;
    }
};
const int M = N+1;

struct node2 {
    mint x[M][M] = {};
    bool unit=0;
    node2() {
        // *this = node(0);
        unit=1;
    }
    node2(char c) {
        c-='a';
        for(int i=0;i<M;++i) {
            x[c][i]=1;
        }
        for(int i=0;i<M;++i) {
            x[i][i]=1;
        }
        
    }
    void merge(const node2& o) {
        if(o.unit) return;
        if(unit) {
            *this=o;
            return;
        }
        node2 res = {};
        res.unit=0;
        for(int i=0;i<M;++i) for(int j=0;j<M;++j) for(int k=0;k<M;++k) {
            res.x[i][k]+=x[i][j]*o.x[j][k];
        }
        *this=res;
    }
    mint ans() const {
        
        mint res=0;
        for(int i=0;i<M-1;++i) {
            res+=x[i][M-1]; // could have stayed...
        }
        return res;
    }
    friend node2 merge(node2 a, const node2& b) {
        a.merge(b);
        return a;
    }
};
template<typename node>
struct segtree {
    int ptwo;
    vector<node> seg;
    segtree(){}
    node& operator[](int i) {
        return seg[i+ptwo];
    }
    segtree(int nn) {
        ptwo=1<<__lg(nn*2-1);
        seg.assign(ptwo*2,{});
    }
    auto query(int l, int r) {
        assert(l>=0 and l<ptwo and r >=0 and r<ptwo);
        l+=ptwo; r+=ptwo;
        node resl, resr;
        while(l and l<=r) {
            if(l%2==1) resl = merge(resl,seg[l++]);
            if(r%2==0) resr = merge(seg[r--],resr);
            l/=2; r/=2;
        }
        return merge(resl,resr);
    }
    void update(int i, char val) {
        assert(i>=0 and i<ptwo);
        i+=ptwo;
        seg[i] = node(val);
        for(i/=2;i>=1;i/=2) {
            seg[i] = merge(seg[2*i],seg[2*i+1]);
        }
    }
    void build() {
        for(int i=ptwo-1;i>=1;--i) {
            seg[i] = merge(seg[2*i],seg[2*i+1]);
        }
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,q; cin >> n >> q;
    string s; cin >> s;
    segtree<node> seg(n);
    segtree<node2> seg2(n);
    for(int i=0;i<n;++i) {
        seg[i]=s[i];
        seg2[i]=s[i];
    }
    seg.build(), seg2.build();
    auto answer = [&]() {
        cout << seg2.seg[1].ans()-seg.seg[1].ans() << '\n';
    };
    answer();
    while(q--) {
        int i; char c; cin >> i >> c;
        --i;
        seg.update(i,c);
        seg2.update(i,c);
        answer();
    }
}