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
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
using namespace std;

#define REP(i,q) for(int i=0; i<(q); ++i)
#define FOR(i,p,q) for(int i=(p); i<=(q); ++i)
#define ROF(i,q,p) for(int i=(q); i>=(p); --i)
#define V vector
#define pb push_back
#define Co const
#define fi first
#define se seconda
#define as assign
#define rz resize
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
#define sz(V) (int)(V.size())
#define ckmin(a,b) a=min(a,b)
#define ckmax(a,b) a=max(a,b)
typedef long double ld;
typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#ifndef UNCLE
 typedef basic_string<bool> vb;
 typedef basic_string<int> vi;
 typedef basic_string<ll> vl;
#else
 typedef V<bool> vb;
 typedef V<int> vi;
 typedef V<ll> vl;
#endif

constexpr ll MOD=998'244'353;
int N,Q, MX_A=1;
vi s;

void Input(){
    string ss;
    cin>>N>>Q>>ss;
    s.rz(N);
    REP(i,N) s[i]=ss[i]-'a', ckmax(MX_A,s[i]+1);
}
void Solv(){
    vl dp1(N+1,0);
    V<vl> dp2(N+1,vl(MX_A,0));
    vi lf(MX_A,-1);
    dp1[0]=1;
    REP(i,N){
        dp1[i+1]=(2*dp1[i]-(lf[s[i]]!=-1?dp1[lf[s[i]]-1+1]:0)+MOD)%MOD;//mod tez
        
        
        if(i==0||s[i-1]!=s[i]){
            if(lf[s[i]]!=-1) dp2[i+1][s[i]]=dp2[lf[s[i]]+1][s[i]];
            else dp2[i+1][s[i]]=1;
            REP(ia,MX_A) if(ia!=s[i]){
                dp2[i+1][ia]=dp2[i][ia];
                if(lf[ia]!=-1&&lf[ia]>lf[s[i]]){
                    dp2[i+1][s[i]]=dp2[i+1][s[i]]+dp2[i][ia];
                    if(dp2[i+1][s[i]]>MOD) dp2[i+1][s[i]]-=MOD;
                }
            }
        }else{
            REP(ia,MX_A) dp2[i+1][ia]=dp2[i][ia];
        }
        lf[s[i]]=i;
    }
    ll res=dp1[N]-1;
    REP(ia,MX_A) res=(res-dp2[N][ia]+MOD)%MOD;
    cout<<res<<"\n";
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    Input();
    Solv();
    while(Q--){
        int x;
        char v;
        cin>>x>>v,--x;
        s[x]=v-'a', ckmax(MX_A,s[x]+1);
        Solv();
    }
}