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
//   _|                                                _|
//   _|  _|    _|    _|    _|_|    _|_|_|      _|_|_|  _|  _|      _|_|      _|_|
//   _|_|      _|    _|  _|_|_|_|  _|    _|  _|_|      _|_|      _|_|_|_|  _|_|_|_|
//   _|  _|    _|    _|  _|        _|    _|      _|_|  _|  _|    _|        _|
//   _|    _|    _|_|_|    _|_|_|  _|_|_|    _|_|_|    _|    _|    _|_|_|    _|_|_|
//                   _|            _|
//               _|_|              _|
#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define md(x) (((x)+M)%M)

typedef int64_t ll;

const ll M = ll(1e9)+7;
const int N = int(2e5)+1000;
ll F[N];
ll iF[N];
ll dp[N][3];
ll bp[N];

ll binpow(ll a, ll k) 
{
    ll res = 1;
    while (k) {
        if (k&1) res = (res*a)%M;
        a = (a*a)%M, k /= 2;
    }
    return res;
}

ll choose(ll a, ll b) {
// #ifdef TEST
//     assert(ir(b, 0, a));
// #endif
    if (b == a) return 1;
    if (b == 0) return 1;
    return (((F[a] * iF[a-b])%M) * iF[b]) % M;
}

void precompute() {
    bp[0] = 1;
    for (ll i = 1; i < N; ++i) bp[i] = md(2*bp[i-1]);
    F[0] = 1;
    for (ll i = 1; i < N; ++i) F[i] = (F[i-1] * i)%M;
    iF[0] = 1;
    for (ll i = 1; i < N; ++i) iF[i] = binpow(F[i], M-2);
    
// #ifdef TEST
//     foru (i, N) assert((F[i] * iF[i])%M == 1);
// #endif
    dp[0][0] = 1;
    for (int i = 1; i < N; ++i) {
        foru (j, 3) {
            dp[i][j] = md(dp[i-1][j] + dp[i-1][(j+2)%3]);
            // if (j == (i+1)%3) dp[i][j] = md(dp[i][j] + choose(i, 
            // TODO: this line is unnecessary, right? (?)
// #define TEST
// #ifdef TEST
//             df("%d\n", i);
//             ll s = 0;
//             for (int x = j; x <= i; x += 3) {
//                 s = md(s + choose(i, x));
//             }
            // cout << i << " " << j << " " << s << " " << dp[i][j] << endl;
//             assert(s == dp[i][j]);
// #endif
        }
    }
}

void compute(const vec<ll>& ct, ll err, ll n) {
    if (n == 1) {
        cout << err << "\n";
        return;
    }
    int starter = ct[0] - ct[1] - ct[2]; // FIXME: check if this is alright (it's not)
    
    starter %= 3;
    while (starter < 0) starter += 3;
    starter %= 3;
    
    ll s0 = dp[ct[2]][starter];
    if (ct[2] == 0) {
        cout << int((starter != 0) && (err == 0)) << "\n";
        return;
    }
    ll actual_err;
    if (n % 2 == 0) actual_err = 0;
    else actual_err = err;

    cout << md(md(bp[ct[2]] - s0) - actual_err) << "\n";
}

int main() {
    df("debug mode\n");
#ifndef DEBUG
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
    precompute();
    int n, q;
    cin >> n >> q;
    
    vec<short> v(n);
    vec<ll> ct = {0, 0, 0};
    
    string str;
    cin >> str;
    foru (i, n) {
        v[i] = (str[i] == 'C') ? 0 : (str[i] == 'Z' ? 1 : 2);
        ++ct[v[i]];
    }
    
    ll e0 = 0, e1 = 0;
    foru (i, n) {
        if (v[i] == 2) ++e0, ++e1;
        else if (v[i] == (i&1)) ++e0;
        else ++e1;
    }
    
    foru (i, n) {
        df("%d", v[i]);
    }
    df("\n");
    
    
    compute(ct, ((e0 == n) + (e1 == n)), n);
    
    
    foru (_, q) {
        int i;
        cin >> i >> str[i-1];
        --i;
        
        if (v[i] == 2) --e0, --e1;
        else if (v[i] == (i&1)) --e0;
        else --e1;
        
        --ct[v[i]];
        
        v[i] = (str[i] == 'C') ? 0 : (str[i] == 'Z' ? 1 : 2);
        
        ++ct[v[i]];
        
        foru (i, n) {
            df("%d", v[i]);
        }
        df("\n");
        if (v[i] == 2) ++e0, ++e1;
        else if (v[i] == (i&1)) ++e0;
        else ++e1;
        
        compute(ct, ((e0 == n) + (e1 == n)), n);
    }
    return 0;
}