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

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
const ll LINF = 1e18;
const int INF = 1e9;

const int N = 200000;
const int MOD = 1e9 + 7;
int binom_sum[N][3];

int cnt_alternating(int cnt_parity[3][2]) {
    return (cnt_parity[0][0] == 0 && cnt_parity[1][1] == 0) +
           (cnt_parity[0][1] == 0 && cnt_parity[1][0] == 0);
}

int mod(int a, int MOD) { return a >= MOD ? a - MOD : a; }

int char_to_int(char c) {
    switch (c) {
    case 'C':
        return 0;
    case 'Z':
        return 1;
    default:
        return 2; // case 'N'
    }
}

int get_result(int cnt[3], int cnt_parity[3][2], int n) {
    int forbidden_k = (cnt[0] - cnt[1] - cnt[2] + 300000) % 3;
    int result = 0;
    for (int i = 0; i < 3; ++i) {
        if (i != forbidden_k) {
            result += binom_sum[cnt[2]][i];
            // cerr << i << ": " << binom_sum[cnt[2]][i] << '\n';
        }
    }
    result = mod(result, MOD);

    int alternating = cnt_alternating(cnt_parity);
    if (n % 2 != 0 && n > 1) {
        // cerr << "Is alternating: " << -alternating << '\n';
        result = mod(MOD + result - alternating, MOD);
    }
    return result;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    binom_sum[0][0] = 1;
    for (int i = 1; i < N; ++i) {
        for (int k0 = 0; k0 < 3; ++k0) {
            binom_sum[i][k0] = binom_sum[i - 1][(k0 + 2) % 3] + binom_sum[i - 1][k0];
            binom_sum[i][k0] = mod(binom_sum[i][k0], MOD);
        }
    }

    int cnt[3] = {0, 0, 0};
    int cnt_parity[3][2] = {0, 0, 0, 0, 0, 0};
    int n, q;
    string s;

    cin >> n >> q;
    cin >> s;

    for (int i = 0; i < n; ++i) {
        int idx = char_to_int(s[i]);
        ++cnt[idx];
        ++cnt_parity[idx][i % 2];
    }

    // cerr << s << endl;
    cout << get_result(cnt, cnt_parity, n) << endl;

    while (q--) {
        int ki;
        char c;
        cin >> ki >> c;

        --ki;
        int old_idx = char_to_int(s[ki]);
        int new_idx = char_to_int(c);
        --cnt[old_idx];
        ++cnt[new_idx];
        --cnt_parity[old_idx][ki % 2];
        ++cnt_parity[new_idx][ki % 2];
        s[ki] = c;

        // cerr << s << endl;
        cout << get_result(cnt, cnt_parity, n) << endl;
    }

    return 0;
}