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
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int mod = 998244353;

const int DIM_MAT = 7;

ll r[DIM_MAT + 2][DIM_MAT + 2];
int tr[DIM_MAT + 2][DIM_MAT + 2];
      
struct Matrix {
   int mat[DIM_MAT][DIM_MAT];
   int n, m;
   Matrix(int n_ = DIM_MAT, int m_ = DIM_MAT) {
      n = n_;
      m = m_;
      for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) mat[i][j] = 0;
   }
   int& operator()(const int i, const int j) { return mat[i][j]; }
   int operator()(const int i, const int j) const { return mat[i][j]; }
   Matrix operator *(const Matrix& x) const {
      for(int i = 0; i < n; i++)
         for(int h = 0; h < x.m; h++)
            r[i][h] = 0;
      
      for(int i = 0; i < x.n; i++) 
         for(int j = 0; j < x.m; j++)
            tr[j][i] = x.mat[i][j];
         
      
      for(int i = 0; i < n; i++)
         for(int j = 0; j < m; j++)
            for(int h = 0; h < x.m; h++)
               r[i][h] += (ll)mat[i][j] * tr[h][j];
      
      Matrix rez(n, x.m);
      for(int i = 0; i < n; i++) 
         for(int j = 0; j < x.m; j++)
            rez.mat[i][j] = r[i][j] % mod;
      return rez;
   }
   bool operator != (const Matrix& x) const {
      if(x.n != n) return 1;
      if(x.m != m) return 1;
      for(int i = 0; i < n; i++) {
         for(int j = 0; j < m; j++) {
            if(mat[i][j] != x.mat[i][j]) return 1;
         }
      }
      return 0;
   }
};

template<typename T>
struct AINT {
   vector<T> aint;
   int n;
   void init(int n_) {
      n = n_;
      aint.assign(n * 2 + 5, T());
   }
   template<class CB> void walk(CB&& cb) { walk(cb, 1, n); }
   template<class CB> void walk(CB&& cb, int l, int r) { walk(cb, l, r, 1, 1, n); }
   template<class CB> void walk(CB&& cb, int l, int r, int node, int cl, int cr) { 
      if(cr < l || r < cl) return;
      if(l <= cl && cr <= r && !cb(aint[node], cl, cr)) return;
      
      int mid = (cl + cr) >> 1, L = node + 1, R = node + (mid - cl + 1) * 2;
      
      walk(cb, l, r, L, cl, mid);
      walk(cb, l, r, R, mid + 1, cr);
      
      aint[node].pull(aint[L], aint[R]);
   }
};

struct Node {
   Matrix m;
   void pull(const Node& a, const Node& b) {
      m = a.m * b.m;
   }
};

struct AMat : AINT<Node> {
   void upd(int p, Matrix x) {
      walk([&](Node &a, int cl, int cr) {
         a.m = x;
         return 0;
      }, p, p);
   }
   Matrix query() {
       return aint[1].m;
   }
};

set<int> app[DIM_MAT];
Matrix get(int p, int c) {
   Matrix rez(DIM_MAT, DIM_MAT);
   for(int i = 0; i < DIM_MAT; i++) if(i != c) rez(i, i) = 1;
   auto it = app[c].lower_bound(p);
   int pr = (it == app[c].begin()? -1: *prev(it));
   for(int i = 0; i < DIM_MAT; i++) {
      it = app[i].lower_bound(p);
      if(it == app[i].begin());
      else {
         if(*prev(it) >= pr) rez(i, c) = 1;
      }
   }
   return rez;
}
Matrix getsimpl(int c) {
   Matrix rez(DIM_MAT, DIM_MAT);
   for(int i = 0; i < DIM_MAT; i++) rez(i, i) = 1;
   for(int i = 0; i < DIM_MAT; i++) rez(i, c) = 1;
   return rez;
}


AMat simple, nonredux;
string s;

void upd(int p, int x) {
   simple.upd(p, getsimpl(x));
   vector<int> needtoupd;
   needtoupd.emplace_back(p);
   for(int i = 0; i < DIM_MAT; i++) {
      auto it = app[i].upper_bound(p);
      if(it != app[i].end()) needtoupd.emplace_back(*it);
   }
   
   vector<Matrix> bef;
   for(auto i : needtoupd) bef.emplace_back(get(i, s[i]));
   
   app[s[p]].erase(p);
   app[x].insert(p);
   s[p] = x;
   
   vector<int> rv; vector<Matrix> T;
   for(int i = 0; i < sz(needtoupd); i++) {
      auto A = get(needtoupd[i], s[needtoupd[i]]);
      if(A != bef[i]) rv.emplace_back(needtoupd[i]), T.emplace_back(A);
   }
   
   for(int i = 0; i < sz(rv); i++)
      nonredux.upd(rv[i], T[i]);
   return;
}

int query() {
   Matrix a(1, DIM_MAT);
   a(0, DIM_MAT - 1) = 1;
   Matrix b = a;
   a = a * simple.query();
   b = b * nonredux.query();
   ll sum = 0;
   
   for(int i = 0; i < DIM_MAT - 1; i++) sum += a(0, i) - b(0, i);
   return (sum % mod + mod) % mod;
}

signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n, q;
   cin >> n >> q;
   simple.init(n);
   nonredux.init(n);
   
   cin >> s;
   for(auto &x : s) x -= 'a';
   s = "$" + s;
   s[0] = DIM_MAT - 1;
   
   for(int i = 0; i < sz(s); i++)
      app[s[i]].emplace(i);
      
   vector<Matrix> fs(sz(s)), fn(sz(s));
   for(int i = 1; i < sz(s); i++) {
      fs[i] = getsimpl(s[i]);
      fn[i] = get(i, s[i]);
   }
   
   simple.walk([&](auto &a, int cl, int cr) {
      if(cl != cr) return 1;
      a.m = fs[cl];
      return 0;
   });
   nonredux.walk([&](auto &a, int cl, int cr) {
      if(cl != cr) return 1;
      a.m = fn[cl];
      return 0;
   });
   
   cout << query() << '\n';
   for(int i = 0, p; i < q; i++) {
      char a;
      cin >> p >> a;
      a -= 'a';
      upd(p, a);
      cout << query() << '\n';
   }
   
   
    
}

/**
      Binecuvanteaza Doamne Ukraina.
--
*/