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
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(c) (c).begin(), (c).end()
#define sz(X) (int)((X).size())
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
#ifdef LOCAL
ostream &operator<<(ostream &out, string str) {
   for (char c : str)
      out << c;
   return out;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; }
template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) {
   auto &[a, b, c] = p;
   return out << "(" << a << ", " << b << ", " << c << ")";
}
template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
   out << '{';
   for (auto it = a.begin(); it != a.end(); it = next(it))
      out << (it != a.begin() ? ", " : "") << *it;
   return out << '}';
}
void dump() { cerr << "\n"; }
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
const int N = 7;
const ll mod = 998244353;
typedef array<array<ll, N>, N> arr;

void mult(const arr &a, const arr &b, arr &c) {
   rep(i, N) rep(j, N) c[i][j] = 0;

   rep(i, N) rep(k, N) rep(j, N) c[i][j] += a[i][k] * b[k][j];
   rep(i, N) rep(j, N) c[i][j] %= mod;
}

arr id = {};
void pre() { rep(i, N) id[i][i] = 1; }

struct ST {
   static constexpr int s = 1 << 16;
   vector<arr> V;
   ST(int n) : V(2 * s, id) { assert(n < s); };

   void update(int idx) { mult(V[idx * 2], V[idx * 2 + 1], V[idx]); }

   void sset(int idx, arr x) {
      idx += s;
      // if (V[idx] == x)
      // return;
      V[idx] = x;
      while (idx /= 2)
         update(idx);
   }
};

arr get_normal(int c) {
   auto a = id;
   rep(i, N) a[i][c] = 1;
   return a;
}

arr get_special(int c, set<int> S) {
   auto a = id;
   for (auto i : S)
      a[i][c] = 1;
   if (!S.contains(c))
      a.back()[c] = 1;
   return a;
}

int solve_naive(string s) {
   unordered_map<string, int> M;
   rep(S, 1 << sz(s)) if (S) {
      string t;
      rep(i, sz(s)) if (S & (1 << i)) t.push_back(s[i]);
      M[t]++;
   }
   int res = 0;
   for (auto &[_, b] : M)
      res += (b > 1);
   return res;
}

int32_t main() {
   _upgrade;
   pre();
   int n, q;
   cin >> n >> q;
   string s;
   // rep(i, n) s.push_back('a' + (rand() % 6));
   cin >> s;

   for (auto &c : s)
      c -= 'a';

   ST normal(n), special(n);

   array<set<int>, N> pos;
   rep(i, n) pos[s[i]].insert(i);

   auto get_impacted = [&](auto x) {
      vector<int> S = {x};
      for (auto &s : pos)
         if (auto itr = s.upper_bound(x); itr != s.end())
            S.push_back(*itr);
      return S;
   };

   auto getS = [&](auto c, auto x) {
      set<int> S;
      auto itr = pos[c].find(x);
      assert(itr != pos[c].end());
      auto prev = itr == pos[c].begin() ? -1 : *(--itr);

      rep(i, sz(pos)) if (auto itr = pos[i].lower_bound(prev); itr != pos[i].end() and *itr < x) S.insert(i);
      return S;
   };

   auto print_answer = [&]() {
      // debug(solve_naive(s));

      auto A = normal.V[1];
      auto B = special.V[1];
      ll res = 0;
      rep(i, N) res += A.back()[i] - B.back()[i];
      auto answer = (res + mod * mod) % mod;
      cout << answer << endl;
   };

   rep(i, n) {
      int c = s[i];
      normal.sset(i, get_normal(c));
      auto S = getS(c, i);
      auto special_matrix = get_special(c, S);
      special.sset(i, special_matrix);
   }
   print_answer();
   while (q--) {
      int i = n / 2;
      char character = 'a';
      cin >> i >> character;
      int c = character - 'a';
      --i;

      pos[s[i]].erase(i);
      s[i] = c;
      pos[s[i]].insert(i);

      normal.sset(i, get_normal(c));

      auto I = get_impacted(i);
      for (auto i : I) {
         auto c = s[i];
         auto S = getS(c, i);
         auto special_matrix = get_special(c, S);
         special.sset(i, special_matrix);
      }
      print_answer();
   }
}

//  int solve_dp(string s) {
//     for (auto &c : s)
//        c -= 'a';
//     array<int, 6> dp1{}, dp2{};
//     array<set<char>, 6> S;

//     for (char c : s) {
//        dp1[c] = accumulate(all(dp1), 1);
//        if (!S[c].contains(c))
//           dp2[c]++;

//        for (auto other : S[c])
//           if (other != c)
//              dp2[c] += dp2[other];

//        S[c].clear();
//        for (auto &s : S)
//           s.insert(c);
//     }

//     return accumulate(all(dp1), 0) - accumulate(all(dp2), 0);
//  }

//  void compare(string s) {
//     auto good = solve_naive(s);
//     auto mine = solve_dp(s);
//     debug(s, good, mine);
//     assert(good == mine);
//  }