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
182
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstdint>

static const uint64_t MODULO = 1000 * 1000 * 1000 + 7;
static const size_t MAX_N = 512 * 1024;

static uint8_t termions[MAX_N];

struct tree_el_t {
    uint32_t modulos[3];
    uint32_t wave_misfits[2];
};

static tree_el_t tree[MAX_N];

void load_termions(int n) {
    int i = 0;
    while (i < n) {
        int c = getchar();
        uint8_t t;
        switch (c) {
            case 'N': t = 0;  break;
            case 'C': t = 1;  break;
            case 'Z': t = 2; break;
            default: continue;
        }
        termions[i] = t;
        i++;
    }
}

void tree_fix_el(int idx) {
    auto& curr = tree[idx];
    const auto& left = tree[2 * idx + 0];
    const auto& right = tree[2 * idx + 1];

    for (int i = 0; i < 3; i++) {
        uint64_t sum = 0;
        for (int j = 0; j < 3; j++) {
            sum += uint64_t(left.modulos[(i + 3 - j) % 3]) * uint64_t(right.modulos[j]);
        }
        curr.modulos[i] = sum % MODULO;
    }

    for (int i = 0; i < 2; i++) {
        curr.wave_misfits[i] = left.wave_misfits[i] + right.wave_misfits[i];
    }
}

int tree_fix_leaf(int tree_depth, int termion_idx, uint8_t t) {
    // printf("TRUE LEAF: %d\n", termion_idx);
    const int idx = (1 << tree_depth) + termion_idx;

    auto& leaf = tree[idx];

    switch (t) {
    case 0:
        leaf.modulos[0] = 0;
        leaf.modulos[1] = 1;
        leaf.modulos[2] = 1;
        break;
    case 1:
        leaf.modulos[0] = 0;
        leaf.modulos[1] = 1;
        leaf.modulos[2] = 0;
        break;
    case 2:
        leaf.modulos[0] = 0;
        leaf.modulos[1] = 0;
        leaf.modulos[2] = 1;
        break;
    default:
        assert(false);
    }

    leaf.wave_misfits[termion_idx % 2] = (t == 1);
    leaf.wave_misfits[1 - (termion_idx % 2)] = (t == 2);

    return idx / 2;
}

void tree_make_dummy_leaf(int tree_depth, int termion_idx) {
    // printf("DUMMY LEAF: %d\n", termion_idx);
    const int idx = (1 << tree_depth) + termion_idx;
    auto& leaf = tree[idx];
    leaf.modulos[0] = 1;
    leaf.modulos[1] = 0;
    leaf.modulos[2] = 0;
    leaf.wave_misfits[0] = 0;
    leaf.wave_misfits[1] = 0;
}

void dump_tree(int tree_depth, int current_depth, int idx) {
    if (current_depth > tree_depth) {
        return;
    }

    dump_tree(tree_depth, current_depth + 1, 2 * idx);

    for (int i = 0; i < current_depth; i++) {
        printf("  ");
    }

    const auto& node = tree[(1 << current_depth) + idx];
    printf("[%u %u %u] [%u %u]\n", node.modulos[0], node.modulos[1], node.modulos[2], node.wave_misfits[0], node.wave_misfits[1]);

    dump_tree(tree_depth, current_depth + 1, 2 * idx + 1);
}

int compute_current_answer(int n) {
    // printf("MISFITS: %u %u\n", tree[1].wave_misfits[0], tree[1].wave_misfits[1]);
    // printf("MODULOS: %u %u %u\n", tree[1].modulos[0], tree[1].modulos[1], tree[1].modulos[2]);
    uint64_t ret = tree[1].modulos[1] + tree[1].modulos[2];
    if (n > 1 && (n % 2) == 1) {
        // We need to account for "waves"
        ret += MODULO;
        ret -= tree[1].wave_misfits[0] == 0;
        ret -= tree[1].wave_misfits[1] == 0;
    }
    return ret % MODULO;
}

int main() {
    int n, q;
    scanf("%d %d", &n, &q);

    // Load data
    load_termions(n);

    // Determine the static tree height
    int tree_depth = 0;
    while (n > (1 << tree_depth)) {
        tree_depth++;
    }

    // printf("Tree depth: %d\n", tree_depth);

    // Populate leaves
    // TODO: We could get away with not keeping termions
    for (int i = 0; i < n; i++) {
        tree_fix_leaf(tree_depth, i, termions[i]);
    }
    // Initialize the rest of the leaf layer
    for (int i = n; i < (1 << tree_depth); i++) {
        tree_make_dummy_leaf(tree_depth, i);
    }

    // Fix inner nodes of the tree
    for (int i = (1 << tree_depth) - 1; i > 0; i--) {
        tree_fix_el(i);
    }

    // dump_tree(tree_depth, 0, 0);
    printf("%d\n", compute_current_answer(n));
    for (int i = 0; i < q; i++) {
        int termion_idx;
        char c;
        scanf("%d %c", &termion_idx, &c);
        termion_idx -= 1;

        uint8_t t;
        switch (c) {
            case 'N': t = 0; break;
            case 'C': t = 1; break;
            case 'Z': t = 2; break;
            default: assert(false);
        }

        int idx = tree_fix_leaf(tree_depth, termion_idx, t);
        while (idx != 0) {
            tree_fix_el(idx);
            idx /= 2;
        }

        // dump_tree(tree_depth, 0, 0);
        printf("%d\n", compute_current_answer(n));
    }

    return 0;
}