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
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif

constexpr int mod = 1e9 + 7;

int add(int a, int b) {
	a += b;
	return a >= mod ? a - mod : a;
}
int sub(int a, int b) {
	return add(a, mod - b);
}
int mul(int a, int b) {
	return int(a * LL(b) % mod);
}
int powi(int a, int b) {
	for(int ret = 1;; b /= 2) {
		if(b == 0)
			return ret;
		if(b & 1)
			ret = mul(ret, a);
		a = mul(a, a);
	}
}
int inv(int x) {
	return powi(x, mod - 2);
}

int convert(char x) {
    if (x == 'C')
        return 0;
    if (x == 'Z')
        return 1;
    if (x == 'N')
        return 2;
    assert(false);
}

vector<int> add_vec(const vector<int>& a, const vector<int>& b) {
    assert(ssize(a) <= 3 and ssize(b) <= 3);
    vector<int> ret(3);
    REP(i, ssize(a)) {
        ret[i] = add(ret[i], a[i]);
    }
    REP(i, ssize(b)) {
        ret[i] = add(ret[i], b[i]);
    }
    return ret;
}

vector<int> mul_vec(const vector<int>& a, const vector<int>& b) {
    assert(ssize(a) <= 3 and ssize(b) <= 3);
    vector<int> ret(3);
    REP(i, ssize(a)) {
        REP(j, ssize(b)) {
            const int p = (i + j) % 3;
            ret[p] = add(ret[p], mul(a[i], b[j]));
        }
    }
    while (ssize(ret) > 3) {
        const int pos = ssize(ret) - 4;
        ret[pos] = add(ret[pos], ret.back());
        ret.pop_back();
    }
    return ret;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

    int n, q;
    cin >> n >> q;

    vector<int> v(n);
    REP(i, n) {
        char x;
        cin >> x;
        v[i] = convert(x);
    }

    vector<pair<int, int>> queries(q);
    REP(i, q) {
        int p;
        char x;
        cin >> p >> x;
        queries[i] = {p - 1, convert(x)};
    }

    if (n == 1) {
        REP(i, q + 1) {
            if (v[0] == 2)
                cout << 2 << '\n';
            else
                cout << 1 << '\n';
            if (i < q) {
                auto [p, x] = queries[i];
                v[p] = x;
            }
        }
        return 0;
    }

    vector prep(n + 1, vector (3, 0));
    {
        vector temp(3, vector<int>{1, 0, 0});
        vector step(3, vector (3, vector (3, 0)));
        REP(i, 3) {
            REP(j, 3) {
                step[i][j][(i * j) % 3] = 1;
            }
        }
        FOR(i, 0, n) {
            REP(j, 3) {
                vector<int> sum;
                REP(k, 3) {
                    sum = add_vec(sum, mul_vec(temp[k], step[j][k]));
                }
                assert(sum[1] == sum[2]);
                prep[i][(3 - j) % 3] = sub(sum[0], sum[1]);
            }
            REP(j, 3) {
                prep[i][j] = mul(prep[i][j], inv(3));
            }
            REP(j, 3) {
                vector<int> h = {1, 0, 0};
                ++h[j];
                temp[j] = mul_vec(temp[j], h);
            }
        }
    }

    vector cnt(3, vector (2, 0));
    auto update = [&](int i, int val) {
        cnt[v[i]][i % 2] += val;
    };

    auto helper = [&](int b, int r) -> int {
        return sub(powi(2, b), prep[b][r]);
    };
    auto calc = [&] {
        int ret = 0;
        const int red   = cnt[0][0] + cnt[0][1];
        const int green = cnt[1][0] + cnt[1][1];
        const int blue  = cnt[2][0] + cnt[2][1];
        const int r = (2 * (green + blue - red + 3 * n)) % 3;
        ret = add(ret, helper(blue, r));
        if (n % 2 == 1) {
            if (cnt[0][0] + cnt[1][1] == 0)
                ret = sub(ret, 1);
            if (cnt[0][1] + cnt[1][0] == 0)
                ret = sub(ret, 1);
        }
        return ret;
    };
    auto answer = [&] {
        cout << calc() << '\n';
    };

    REP(i, n) {
        update(i, 1);
    }
    answer();

    for (auto [p, x] : queries) {
        update(p, -1);
        v[p] = x;
        update(p, 1);
        answer();
    }
}