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

using namespace std;

typedef long long ll;

const ll mod = 1e9+7;

int main(){
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    
    map<char, int> cnt[2];
    vector<ll> p2 = {1};
    for(int i = 0; i < n; i++){
        p2.push_back(p2.back() * 2);
        p2.back() %= mod;
    }
    vector<vector<ll>> dp(n + 1, vector<ll>(3, 0));
    dp[0] = {1, 0, 0};
    for(int l = 1; l <= n; l++){
        for(int i = 0; i < 3; i++){
            dp[l][i] = dp[l - 1][(i + 1) % 3] + dp[l - 1][(i + 2) % 3];
            dp[l][i] %= mod;
        }
    }
    int b = 0;
    int g = 0;
    int r = 0;
    for(int i = 0; i < n; i++){
        if(s[i] == 'N')
            b++;
        if(s[i] == 'C')
            r++;
        if(s[i] == 'Z')
            g++;
        cnt[i % 2][s[i]]++;
    }
    ll ans = (p2[b] - dp[b][(((r - g) % 3) + 3) % 3] + mod) % mod;
    if(n % 2 and cnt[0]['Z'] == 0 and cnt[1]['C'] == 0)
        ans--;
    if(n % 2 and cnt[1]['Z'] == 0 and cnt[0]['C'] == 0)
        ans--;
    cout << ans << '\n';
    while(q--){
        int p;
        char c;
        cin >> p >> c;
        p--;
        cnt[p % 2][s[p]]--;
        if(s[p] == 'N')
            b--;
        if(s[p] == 'C')
            r--;
        if(s[p] == 'Z')
            g--;
        s[p] = c;
        cnt[p % 2][s[p]]++;
        if(s[p] == 'N')
            b++;
        if(s[p] == 'C')
            r++;
        if(s[p] == 'Z')
            g++;


        ans = (p2[b] - dp[b][(((r - g) % 3) + 3) % 3] + mod) % mod;
        if(n % 2 and cnt[0]['Z'] == 0 and cnt[1]['C'] == 0)
            ans--;
        if(n % 2 and cnt[1]['Z'] == 0 and cnt[0]['C'] == 0)
            ans--;
        cout << ans << '\n';
    }
    return 0;
}