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

using namespace std;

int mod = 998244353;

int n, q;
int word[50003];
int uniqueDp[50003];
int dp[50003][6][1 << 6];
int last[50003];
int occurences[500003][6];

int calculate(int startIndex)
{
    // source:
    // https://www.geeksforgeeks.org/count-distinct-subsequences/
    uniqueDp[0] = 1;

    for (int i = startIndex; i <= n; ++i)
    {
        for (int j = 0; j < 6; ++j) occurences[i][j] = occurences[i - 1][j];
        uniqueDp[i] = (uniqueDp[i - 1] * 2) % mod;

        if (occurences[i][word[i]] != -1)
        {
            uniqueDp[i] = (uniqueDp[i] + mod - uniqueDp[occurences[i][word[i]]]) % mod;
        }
        occurences[i][word[i]] = i - 1;
    }
    

    vector<bool> appeared(7);

    for (int i = 1; i <= n; ++i)
    {   
        last[i] = last[i - 1];
        if (word[i] == word[i - 1])
            continue;

        for (int j = 0; j < 6; ++j)
            for (int k = 0; k < (1 << 6); ++k)
                dp[i][j][k] = 0;

        if (!appeared[word[i]]) dp[i][word[i]][0] = 1;
        appeared[word[i]] = true;

        for (int j = 0; j < (1 << 6); ++j)
        {
            if (j & (1 << word[i])) continue;
            for (int k = 0; k < 6; ++k)
            dp[i][word[i]][0] = (dp[i][word[i]][0] + dp[last[i - 1]][k][j]) % mod;
        }

        for (int j = 0; j < (1 << 6); ++j)
        {
            for (int k = 0; k < 6; ++k)
            {
                if (k == word[i]) continue;
                dp[i][k][j | (1 << word[i])] = (dp[i][k][j | (1 << word[i])] + dp[last[i - 1]][k][j]) % mod;
            }
        }
        last[i] = i;
    }

    int sum = 0;
    for (int j = 0; j < 6; ++j)
        for (int k = 0; k < (1 << 6); ++k)
            sum = (sum + dp[last[n]][j][k]) % mod;

    return (uniqueDp[n] + mod - sum - 1) % mod;
}

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

    cin >> n >> q;

    word[0] = -1;

    for (int i = 1; i <= n; ++i)
    {
        char c;
        cin >> c;
        word[i] = c - 'a';
    }

    for (int i = 0; i <= n; ++i)
    {
        for (int j = 0; j < 6; ++j)
            occurences[i][j] = -1;
    }
    
    cout << calculate(1) << '\n';

    for (int i = 0; i < q; ++i)
    {
        int p;
        char z;
        cin >> p >> z;
        word[p] = z - 'a';
        cout << calculate(p) << '\n';
    }
}