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
// wzt2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//


#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <vector>

struct change
{
	change(int p, char t)
	{
		pos = p;
		ter = t;
	}

	int pos;
	char ter;
};

struct cSum
{
	cSum() : cSum(0, 0, 0) {};

	cSum(int e0, int e1, int e2)
	{
		empty0 = e0;
		empty1 = e1;
		empty2 = e2;
	}

	int empty0;
	int empty1;
	int empty2;
};

const int MOD = 1e9+7;

int n, q;
char* termions = new char[200'001];
cSum* cSums = new cSum[200'001];

int maxSumIndex = -1;
auto changes = std::vector<change>();
int N, C, Z, waveC, waveZ;

int getCoficientsSum()
{
	if (maxSumIndex < 0)
	{
		cSums[0] = cSum(0, 1, 1);
		maxSumIndex = 0;
	}

	for (int i = maxSumIndex + 1; i <= N; i++)
	{
		cSum cS{};
		cS.empty0 = (cSums[i - 1].empty0 + cSums[i - 1].empty2) % MOD;
		cS.empty1 = (cSums[i - 1].empty0 + cSums[i - 1].empty1) % MOD;
		cS.empty2 = (cSums[i - 1].empty1 + cSums[i - 1].empty2) % MOD;
		cSums[i] = cS;
		maxSumIndex = i;
	}

	int diff = (C - Z - N + 999'999) % 3;

	//fprintf(stderr, "N=%d, e0=%d, e1=%d, e2=%d, diff=%d\n", N, cSums[N].empty0, cSums[N].empty1, cSums[N].empty2, diff);

	switch (diff)
	{
	case 0:
		return cSums[N].empty0;
	case 1:
		return cSums[N].empty1;
	case 2:
		return cSums[N].empty2;
	}

	return 0;
}

int calcPosibilities()
{
	int pos = getCoficientsSum();
	if (waveC == n)
	{
		if (n>1 && n & 1) pos = (pos + MOD - 1) % MOD;
	}
	if (waveZ == n)
	{
		if (n > 1 && n & 1) pos = (pos + MOD - 1) % MOD;
	}

	return pos;
}

int main()
{
	scanf("%d %d\n", &n, &q);
	scanf("%s", termions);
	char c[2];
	int pos;
	for (int i = 0; i < q; i++)
	{
		scanf("%d %s", &pos, c);
		changes.push_back(change(pos, c[0]));
	}

	N = C = Z = waveC = waveZ = 0;
	for (int i = 0; i < n; i++)
	{
		char c = termions[i];
		if (c == 'N')
		{
			N++;
			waveC++;
			waveZ++;
		}
		else if (c == 'C')
		{
			C++;
			if (i & 1) waveZ++;
			else waveC++;
		}
		else if (c == 'Z')
		{
			Z++;
			if (i & 1) waveC++;
			else waveZ++;
		}
	}

	printf("%d\n", calcPosibilities());

	for (int i = 0; i < q; i++)
	{
		char ter = changes[i].ter;
		int pos = changes[i].pos - 1;

		if (termions[pos] == 'N')
		{
			N--;
			waveC--;
			waveZ--;
		}
		else if (termions[pos] == 'C')
		{
			C--;
			if (pos & 1) waveZ--;
			else waveC--;
		}
		else if (termions[pos] == 'Z')
		{
			Z--;
			if (pos & 1) waveC--;
			else waveZ--;
		}

		termions[pos] = ter;
		if (ter == 'N')
		{
			N++;
			waveC++;
			waveZ++;
		}
		else if (ter == 'C')
		{
			C++;
			if (pos & 1) waveZ++;
			else waveC++;
		}
		else if (ter == 'Z')
		{
			Z++;
			if (pos & 1) waveC++;
			else waveZ++;
		}
	
		printf("%d\n", calcPosibilities());
	}

	return 0;
}