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
#include <bits/stdc++.h>
// #define MULTIPLE_TESTS
// #define ENDLESS_TESTS
#define TIME_LIMIT 2
// #define MEMORY_LIMIT 512

using namespace std;

#define MODULO 1'000'000'007

struct ModuloCheck 
{
    int m_mod = 0;
    int m_ns = 0;
    vector<array<int, 3>> m_counts;

    ModuloCheck(int length)
    {
        m_mod = length % 3;
        m_ns = 0;
        
        m_counts.reserve(length+1);
        m_counts.push_back({1, 0, 0});

        for (int i = 0; i < length; ++i)
        {
            const auto &b = m_counts[i];
            m_counts.push_back({
                (b[0] + b[2]) % MODULO,
                (b[1] + b[0]) % MODULO, 
                (b[2] + b[1]) % MODULO
            });
        }
    }

    void update(int, char old, char value)
    {
        if (old == 'N') m_ns -= 1;
        if (value == 'N') m_ns += 1;

        if (old == 'Z') m_mod = (m_mod+2) % 3;
        if (value == 'Z') m_mod = (m_mod+1) % 3;
    }

    int count() const 
    {
        const int a = (4-m_mod) % 3;
        const int b = (5-m_mod) % 3;
        const auto &ret = m_counts[m_ns];

        // cout << endl;
        // cout << "m_mod = " << m_mod << endl;
        // cout << "m_ns = " << m_ns << endl;
        // cout << "ret = " << ret[0] << ' ' << ret[1] << ' ' << ret[2] << endl;
        // cout << "ab = " << a << ' ' << b << endl;

        return (ret[a] + ret[b]) % MODULO;
    }
};

struct AlternatingCheck 
{
    int m_mismatches1 = 0;
    int m_mismatches2 = 0;

    AlternatingCheck(int length)
    {
        if (length % 2 == 0)
        {
            m_mismatches1 = m_mismatches2 = length + 1;
        }
        else
        {
            m_mismatches1 = length / 2;
            m_mismatches2 = length - m_mismatches1;
        }
    }

    void update(int index, char old, char value)
    {
        const char expected = "CZ"[index % 2];
        
        if (old != 'N')
        {
            if (old != expected) m_mismatches1 -= 1;
            else m_mismatches2 -= 1;
        }
        
        if (value != 'N')
        {
            if (value != expected) m_mismatches1 += 1;
            else m_mismatches2 += 1;
        }
    }

    int count() const 
    {
        // cout << endl;
        // cout << "m_mismatches1 = " << m_mismatches1 << endl;
        // cout << "m_mismatches2 = " << m_mismatches2 << endl;

        return (m_mismatches1 == 0 ? 1 : 0) + (m_mismatches2 == 0 ? 1 : 0) - MODULO;
    }
};

void test()
{
    int length, changes;
    cin >> length >> changes;

    string data;
    data.reserve(length+5);

    cin >> data;

    ModuloCheck check1(length);
    AlternatingCheck check2(length);

    for (int i = 0; i < length; ++i)
    {
        check1.update(i, 'C', data[i]);
        check2.update(i, 'C', data[i]);
    }

    cout << (check1.count() - check2.count()) % MODULO << '\n';

    for (int i = 0; i < changes; ++i)
    {
        int index;
        char c;
        cin >> index >> std::ws >> c >> std::ws;
        index -= 1;

        check1.update(index, data[index], c);
        check2.update(index, data[index], c);
        data[index] = c;
    
        cout << (check1.count() - check2.count()) % MODULO << '\n';
    }    
}


int main()
{
#ifndef CONTEST_WORKSPACE
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

#ifdef ENDLESS_TESTS
    while(!(cin >> std::ws).eof())
        test();
#else
    int T = 0;

#ifdef MULTIPLE_TESTS
    cin >> T;
#else
    T = 1;
#endif

    while (T --> 0)
        test();
#endif

    return 0;
}