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

using namespace std;

const int N = 50004;
const int mod = 998244353;

char slowo[N];
long long uni_z_pocz[N];

long long count_unikalne(int n) {
    vector<char> v;
    v.push_back(slowo[1]);
    for(int i = 2; i <= n; i++) {
        if(v.back() != slowo[i]) v.push_back(slowo[i]);
    }

    int s = v.size();
    int last_a = s;
    int last_b = s;
    int last_c = s;
    int last_d = s;
    int last_e = s;
    int last_f = s;

    uni_z_pocz[s] = 0;
    for(int i = s-1; i >= 0; i--) {
        char first = v[i];
        uni_z_pocz[i] = 0;
        
        int last_obecny;
        
        if(first == 'a') last_obecny = last_a;
        else if(first == 'b') last_obecny = last_b;
        else if(first == 'c') last_obecny = last_c;
        else if(first == 'd') last_obecny = last_d;
        else if(first == 'e') last_obecny = last_e;
        else last_obecny = last_f;

        if(last_a <= last_obecny) uni_z_pocz[i] += uni_z_pocz[last_a];
        if(last_b <= last_obecny) uni_z_pocz[i] += uni_z_pocz[last_b];
        if(last_c <= last_obecny) uni_z_pocz[i] += uni_z_pocz[last_c];
        if(last_d <= last_obecny) uni_z_pocz[i] += uni_z_pocz[last_d];
        if(last_e <= last_obecny) uni_z_pocz[i] += uni_z_pocz[last_e];
        if(last_f <= last_obecny) uni_z_pocz[i] += uni_z_pocz[last_f];

        if(last_obecny == s) uni_z_pocz[i]++;
        
        uni_z_pocz[i] %= mod;

        if(first == 'a') last_a = i;
        else if(first == 'b') last_b = i;
        else if(first == 'c') last_c = i;
        else if(first == 'd') last_d = i;
        else if(first == 'e') last_e = i;
        else last_f = i;
    }

    int first_a = s, first_b = s, first_c = s, first_d = s, first_e = s, first_f = s;
    long long uni = 0;

    for(int i = 0; i <= s-1; i++) {
        char first = v[i];
        if(first == 'a' && first_a == s) {
            first_a = i;
            uni += uni_z_pocz[i];
        }
        else if(first == 'b' && first_b == s) {
            first_b = i;
            uni += uni_z_pocz[i];
        }
        else if(first == 'c' && first_c == s) {
            first_c = i;
            uni += uni_z_pocz[i];
        }
        else if(first == 'd' && first_d == s) {
            first_d = i;
            uni += uni_z_pocz[i];
        }
        else if(first == 'e' && first_e == s) {
            first_e = i;
            uni += uni_z_pocz[i];
        }
        else if(first == 'f' && first_f == s) {
            first_f = i;
            uni += uni_z_pocz[i];
        }

        uni %= mod;   
    }

    return uni;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    for(int i = 1; i <= n; i++) cin >> slowo[i];

    // liczba wszystkich roznych na poczatku
    long long wszystkie_na_a = 0;
    long long wszystkie_na_b = 0;
    long long wszystkie_na_c = 0;
    long long wszystkie_na_d = 0;
    long long wszystkie_na_e = 0;
    long long wszystkie_na_f = 0;
    for(int i = n; i >= 1; i--) {
        long long suma = (wszystkie_na_a + wszystkie_na_b + wszystkie_na_c + wszystkie_na_d + wszystkie_na_e + wszystkie_na_f + 1) % mod;
        if(slowo[i] == 'a') {
            wszystkie_na_a = suma;
        }
        else if(slowo[i] == 'b') {
            wszystkie_na_b = suma;
        }
        else if(slowo[i] == 'c') {
            wszystkie_na_c = suma;
        }
        else if(slowo[i] == 'd') {
            wszystkie_na_d = suma;
        }
        else if(slowo[i] == 'e') {
            wszystkie_na_e = suma;
        }
        else if(slowo[i] == 'f') {
            wszystkie_na_f = suma;
        }
    }

    long long wszystkie_rozne = (wszystkie_na_a + wszystkie_na_b + wszystkie_na_c + wszystkie_na_d + wszystkie_na_e + wszystkie_na_f) % mod;
    long long unikalne = count_unikalne(n);
    long long ans = wszystkie_rozne - unikalne;
    ans = (ans + mod) % mod;
    cout << ans << "\n";
    //

    for(int i = 1; i <= q; i++) {
        int pos;
        char litera;
        cin >> pos >> litera;
        
        slowo[pos] = litera;
        
        wszystkie_na_a = 0;
        wszystkie_na_b = 0;
        wszystkie_na_c = 0;
        wszystkie_na_d = 0;
        wszystkie_na_e = 0;
        wszystkie_na_f = 0;
        for(int i = n; i >= 1; i--) {
            long long suma = (wszystkie_na_a + wszystkie_na_b + wszystkie_na_c + wszystkie_na_d + wszystkie_na_e + wszystkie_na_f + 1) % mod;
            if(slowo[i] == 'a') {
                wszystkie_na_a = suma;
            }
            else if(slowo[i] == 'b') {
                wszystkie_na_b = suma;
            }
            else if(slowo[i] == 'c') {
                wszystkie_na_c = suma;
            }
            else if(slowo[i] == 'd') {
                wszystkie_na_d = suma;
            }
            else if(slowo[i] == 'e') {
                wszystkie_na_e = suma;
            }
            else if(slowo[i] == 'f') {
                wszystkie_na_f = suma;
            }
        }

        wszystkie_rozne = (wszystkie_na_a + wszystkie_na_b + wszystkie_na_c + wszystkie_na_d + wszystkie_na_e + wszystkie_na_f) % mod;
        unikalne = count_unikalne(n);
        ans = wszystkie_rozne - unikalne;
        ans = (ans + mod) % mod;
        cout << ans << "\n";
    }

    return 0;
}