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
#include<iostream>
#include<string>
using namespace std;

const int N = 200005;
const int MOD = 1000000007;
long long tab[3][N];

long long calc(int r, int g, int b, int z0, int c0, int z1, int c1) {
    long long diff = abs(r - g);
    long long res = 0;
    if (b > 0) {
        for (int i = 0; i < 3; i++) {
            long long mod = (diff + i + b) % 3;
            if (mod != 0) {
                res += tab[i][b];
                res %= MOD;
                if (mod == 1 && (r + g + b) % 2 == 1) {
                    if (z0 == 0 && c1 == 0) {
                        res = (res - 1) % MOD;
                    }
                    if (z1 == 0 && c0 == 0) {
                        res = (res - 1) % MOD;
                    }
                }
            }
        }
    }
    return res;
}

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

    // Pascal's triangle aggregated to mod 3 elems
    tab[0][1] = 1;
    tab[1][1] = 1;
    tab[2][1] = 0;
    for (int i = 2; i < N; i++) {
        tab[0][i] = (tab[0][i - 1] + tab[2][i - 1]) % MOD;
        tab[1][i] = (tab[1][i - 1] + tab[0][i - 1]) % MOD;
        tab[2][i] = (tab[2][i - 1] + tab[1][i - 1]) % MOD;
    }    
    
    // Reading n, q, s
    int n, q;
    string s;
    cin >> n >> q >> s;

    // Counting how many reds, greens and blues we have
    int r = 0, g = 0, b = 0;
    for (int i = 0; i < n; i++){
        if (s[i] == 'N') b++;
        else if (s[i] == 'C') r++;
        else g++;
    }

    // Counting how many greens and reds we have on even and odd indexes 
    int z0 = 0, c0 = 0, z1 = 0, c1 = 0;
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0) {
            if (s[i] == 'Z') z0++;
            else if (s[i] == 'C') c0++;
        }
        else {
            if (s[i] == 'Z') z1++;
            else if (s[i] == 'C') c1++;
        }
    }
    
    cout << calc(r, g, b, z0, c0, z1, c1) << '\n';
    while (q--) {
        int pos;
        char c;
        cin >> pos >> c;
        pos--;
        
        // Update how many reds, greens and blues we have
        if (s[pos] == 'N') b--;
        else if (s[pos] == 'C') r--;
        else g--;        
        if (c == 'N') b++;
        else if (c == 'C') r++;
        else g++;
        
        // Updating how many greens and reds we have on even and odd indexes 
        if (pos % 2 == 0) {
            if (s[pos] == 'Z') z0--;
            if (s[pos] == 'C') c0--;
            if (c == 'Z') z0++;
            if (c == 'C') c0++;
        }
        else {
            if (s[pos] == 'Z') z1--;
            if (s[pos] == 'C') c1--;
            if (c == 'Z') z1++;
            if (c == 'C') c1++;
        }

        s[pos] = c;
        cout << calc(r, g, b, z0, c0, z1, c1) << '\n';
    }
    cout << flush;

    return 0;
}