#include <bits/stdc++.h> using namespace std; const int N = 200100; const long long mod = 1e9+7; int a[N]; struct node { long long dp[2][2]; long long dpSingle[2]; long long dpDouble[2]; long long dpTriple[2]; long long dpAnd[2]; }; node t[N*4]; /* lU lR lG rU rR rG merge rules: odd_x + odd_x -> ~x odd_x + even_x -> x even_x + odd_~x -> ~x even_x + even_~x -> x and ~x odd_x + odd~x -> even_~x odd_x + even~x -> odd_x even_x + odd~x -> odd_x even_x + even_x -> even_x dpSingle[2]; dpDouble[2]; dpTriple[2]; dpAnd[2]; dpDoubleAnd[2]; */ node merge(node l, node r) { node ret; for (int i = 0; i < 2; i++) ret.dpSingle[i] = ret.dpDouble[i] = ret.dpTriple[i] = ret.dpAnd[i] = ret.dp[i][0] = ret.dp[i][1] = 0; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[0] * r.dpSingle[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpSingle[0] * r.dpDouble[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dpTriple[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dpAnd[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[0] * r.dp[0][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dp[0][0]) % mod; ret.dpAnd[0] = (ret.dpAnd[0] + l.dpSingle[0] * r.dpSingle[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[0] * r.dpDouble[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dpTriple[1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpSingle[0] * r.dpAnd[1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dpSingle[0] * r.dp[1][1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpSingle[0] * r.dp[1][0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpDouble[0] * r.dpSingle[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[0] * r.dpDouble[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpTriple[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpAnd[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpDouble[0] * r.dp[0][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dp[0][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[0] * r.dpSingle[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpDouble[0] * r.dpDouble[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpTriple[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpAnd[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[0] * r.dp[1][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[0] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[0] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[0] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[0] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[0] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[0] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[0] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[0] * r.dp[1][0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpAnd[0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[0] * r.dpTriple[0]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dpAnd[0] * r.dpAnd[0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpAnd[0] * r.dp[0][1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dpAnd[0] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[0] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[0] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[0] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[0] * r.dp[1][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][1] * r.dpSingle[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][1] * r.dpDouble[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dpTriple[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dpAnd[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][1] * r.dp[0][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dp[0][0]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][1] * r.dpSingle[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][1] * r.dpDouble[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dpTriple[1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][1] * r.dpAnd[1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][1] * r.dp[1][1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][1] * r.dp[1][0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][0] * r.dpTriple[0]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][0] * r.dpAnd[0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][0] * r.dp[0][1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][0] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[0][0] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][0] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][0] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][0] * r.dp[1][0]) % mod; ret.dpAnd[1] = (ret.dpAnd[1] + l.dpSingle[1] * r.dpSingle[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[1] * r.dpDouble[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dpTriple[0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpSingle[1] * r.dpAnd[0]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dpSingle[1] * r.dp[0][1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpSingle[1] * r.dp[0][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[1] * r.dpSingle[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpSingle[1] * r.dpDouble[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dpTriple[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dpAnd[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[1] * r.dp[1][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dp[1][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[1] * r.dpSingle[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpDouble[1] * r.dpDouble[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpTriple[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpAnd[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[1] * r.dp[0][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dp[0][0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpDouble[1] * r.dpSingle[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[1] * r.dpDouble[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpTriple[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpAnd[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpDouble[1] * r.dp[1][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[1] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[1] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[1] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[1] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[1] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[1] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[1] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[1] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[1] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[1] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[1] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[1] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[1] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[1] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[1] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[1] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[1] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[1] * r.dp[0][0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpAnd[1] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[1] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[1] * r.dpTriple[1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dpAnd[1] * r.dpAnd[1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpAnd[1] * r.dp[1][1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dpAnd[1] * r.dp[1][0]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][1] * r.dpSingle[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][1] * r.dpDouble[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dpTriple[0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][1] * r.dpAnd[0]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][1] * r.dp[0][1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][1] * r.dp[0][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][1] * r.dpSingle[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][1] * r.dpDouble[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dpTriple[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dpAnd[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][1] * r.dp[1][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[1][0] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][0] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][0] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][0] * r.dp[0][0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][0] * r.dpTriple[1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][0] * r.dpAnd[1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][0] * r.dp[1][1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][0] * r.dp[1][0]) % mod; return ret; } void build(int v, int l, int r) { if (l == r) { t[v].dpSingle[0] = t[v].dpSingle[1] = 0; if (a[l] == 2) t[v].dpSingle[0] = t[v].dpSingle[1] = 1; else t[v].dpSingle[a[l]] = 1; return; } int mid = (l+r)>>1; build(v+v, l, mid); build(v+v+1, mid+1, r); t[v] = merge(t[v+v], t[v+v+1]); } void update(int v, int l, int r, int pos, int x) { if (l == r) { t[v].dpSingle[0] = t[v].dpSingle[1] = 0; if (x == 2) t[v].dpSingle[0] = t[v].dpSingle[1] = 1; else t[v].dpSingle[x] = 1; return; } int mid = (l+r)>>1; if (pos <= mid) update(v+v, l, mid, pos, x); else update(v+v+1, mid+1, r, pos, x); t[v] = merge(t[v+v], t[v+v+1]); } long long get_ans() { long long ans = (t[1].dpSingle[0] + t[1].dpSingle[1]) % mod; ans = (ans + t[1].dpDouble[0] + t[1].dpDouble[1]) % mod; return ans; } void solve() { int n, tt; cin >> n >> tt; for (int i = 0; i < n; i++) { char c; cin >> c; if (c == 'Z') a[i] = 0; else if (c == 'C') a[i] = 1; else a[i] = 2; } build(1, 0, n-1); cout << get_ans() << "\n"; while(tt--) { int pos; char c; cin >> pos >> c; --pos; int x; if (c == 'Z') x = 0; else if (c == 'C') x = 1; else x = 2; update(1, 0, n-1, pos, x); cout << get_ans() << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; t = 1; #ifdef Local // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // cin >> t; #endif // cin >> t; while(t--){ solve(); } return 0; }
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | #include <bits/stdc++.h> using namespace std; const int N = 200100; const long long mod = 1e9+7; int a[N]; struct node { long long dp[2][2]; long long dpSingle[2]; long long dpDouble[2]; long long dpTriple[2]; long long dpAnd[2]; }; node t[N*4]; /* lU lR lG rU rR rG merge rules: odd_x + odd_x -> ~x odd_x + even_x -> x even_x + odd_~x -> ~x even_x + even_~x -> x and ~x odd_x + odd~x -> even_~x odd_x + even~x -> odd_x even_x + odd~x -> odd_x even_x + even_x -> even_x dpSingle[2]; dpDouble[2]; dpTriple[2]; dpAnd[2]; dpDoubleAnd[2]; */ node merge(node l, node r) { node ret; for (int i = 0; i < 2; i++) ret.dpSingle[i] = ret.dpDouble[i] = ret.dpTriple[i] = ret.dpAnd[i] = ret.dp[i][0] = ret.dp[i][1] = 0; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[0] * r.dpSingle[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpSingle[0] * r.dpDouble[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dpTriple[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dpAnd[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[0] * r.dp[0][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dp[0][0]) % mod; ret.dpAnd[0] = (ret.dpAnd[0] + l.dpSingle[0] * r.dpSingle[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[0] * r.dpDouble[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[0] * r.dpTriple[1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpSingle[0] * r.dpAnd[1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dpSingle[0] * r.dp[1][1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpSingle[0] * r.dp[1][0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpDouble[0] * r.dpSingle[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[0] * r.dpDouble[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpTriple[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpAnd[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpDouble[0] * r.dp[0][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dp[0][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[0] * r.dpSingle[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpDouble[0] * r.dpDouble[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpTriple[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dpAnd[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[0] * r.dp[1][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[0] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[0] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[0] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[0] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[0] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[0] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[0] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[0] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[0] * r.dp[1][0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpAnd[0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[0] * r.dpTriple[0]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dpAnd[0] * r.dpAnd[0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dpAnd[0] * r.dp[0][1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dpAnd[0] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[0] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[0] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[0] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[0] * r.dp[1][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][1] * r.dpSingle[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][1] * r.dpDouble[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dpTriple[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dpAnd[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][1] * r.dp[0][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dp[0][0]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][1] * r.dpSingle[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][1] * r.dpDouble[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][1] * r.dpTriple[1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][1] * r.dpAnd[1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][1] * r.dp[1][1]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][1] * r.dp[1][0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][0] * r.dpTriple[0]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][0] * r.dpAnd[0]) % mod; ret.dp[0][1] = (ret.dp[0][1] + l.dp[0][0] * r.dp[0][1]) % mod; ret.dp[0][0] = (ret.dp[0][0] + l.dp[0][0] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[0][0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[0][0] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][0] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[0][0] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[0][0] * r.dp[1][0]) % mod; ret.dpAnd[1] = (ret.dpAnd[1] + l.dpSingle[1] * r.dpSingle[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[1] * r.dpDouble[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dpTriple[0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpSingle[1] * r.dpAnd[0]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dpSingle[1] * r.dp[0][1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpSingle[1] * r.dp[0][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[1] * r.dpSingle[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpSingle[1] * r.dpDouble[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dpTriple[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dpAnd[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpSingle[1] * r.dp[1][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpSingle[1] * r.dp[1][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[1] * r.dpSingle[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpDouble[1] * r.dpDouble[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpTriple[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpAnd[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[1] * r.dp[0][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dp[0][0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpDouble[1] * r.dpSingle[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpDouble[1] * r.dpDouble[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpTriple[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dpAnd[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpDouble[1] * r.dp[1][1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpDouble[1] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[1] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[1] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[1] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[1] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[1] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[1] * r.dp[0][0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[1] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpTriple[1] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpTriple[1] * r.dpTriple[1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[1] * r.dpAnd[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpTriple[1] * r.dp[1][1]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpTriple[1] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[1] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dpAnd[1] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dpAnd[1] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[1] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[1] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[1] * r.dp[0][0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpAnd[1] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dpAnd[1] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dpAnd[1] * r.dpTriple[1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dpAnd[1] * r.dpAnd[1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dpAnd[1] * r.dp[1][1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dpAnd[1] * r.dp[1][0]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][1] * r.dpSingle[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][1] * r.dpDouble[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dpTriple[0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][1] * r.dpAnd[0]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][1] * r.dp[0][1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][1] * r.dp[0][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][1] * r.dpSingle[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][1] * r.dpDouble[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dpTriple[1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dpAnd[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][1] * r.dp[1][1]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][1] * r.dp[1][0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][0] * r.dpSingle[0]) % mod; ret.dpDouble[0] = (ret.dpDouble[0] + l.dp[1][0] * r.dpDouble[0]) % mod; ret.dpTriple[0] = (ret.dpTriple[0] + l.dp[1][0] * r.dpTriple[0]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][0] * r.dpAnd[0]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][0] * r.dp[0][1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][0] * r.dp[0][0]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][0] * r.dpSingle[1]) % mod; ret.dpDouble[1] = (ret.dpDouble[1] + l.dp[1][0] * r.dpDouble[1]) % mod; ret.dpTriple[1] = (ret.dpTriple[1] + l.dp[1][0] * r.dpTriple[1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][0] * r.dpAnd[1]) % mod; ret.dp[1][1] = (ret.dp[1][1] + l.dp[1][0] * r.dp[1][1]) % mod; ret.dp[1][0] = (ret.dp[1][0] + l.dp[1][0] * r.dp[1][0]) % mod; return ret; } void build(int v, int l, int r) { if (l == r) { t[v].dpSingle[0] = t[v].dpSingle[1] = 0; if (a[l] == 2) t[v].dpSingle[0] = t[v].dpSingle[1] = 1; else t[v].dpSingle[a[l]] = 1; return; } int mid = (l+r)>>1; build(v+v, l, mid); build(v+v+1, mid+1, r); t[v] = merge(t[v+v], t[v+v+1]); } void update(int v, int l, int r, int pos, int x) { if (l == r) { t[v].dpSingle[0] = t[v].dpSingle[1] = 0; if (x == 2) t[v].dpSingle[0] = t[v].dpSingle[1] = 1; else t[v].dpSingle[x] = 1; return; } int mid = (l+r)>>1; if (pos <= mid) update(v+v, l, mid, pos, x); else update(v+v+1, mid+1, r, pos, x); t[v] = merge(t[v+v], t[v+v+1]); } long long get_ans() { long long ans = (t[1].dpSingle[0] + t[1].dpSingle[1]) % mod; ans = (ans + t[1].dpDouble[0] + t[1].dpDouble[1]) % mod; return ans; } void solve() { int n, tt; cin >> n >> tt; for (int i = 0; i < n; i++) { char c; cin >> c; if (c == 'Z') a[i] = 0; else if (c == 'C') a[i] = 1; else a[i] = 2; } build(1, 0, n-1); cout << get_ans() << "\n"; while(tt--) { int pos; char c; cin >> pos >> c; --pos; int x; if (c == 'Z') x = 0; else if (c == 'C') x = 1; else x = 2; update(1, 0, n-1, pos, x); cout << get_ans() << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; t = 1; #ifdef Local // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // cin >> t; #endif // cin >> t; while(t--){ solve(); } return 0; } |