Problem:
Score : 700 points

Problem Statement

You are given a string S consisting of a,b and c. Find the number of strings that can be possibly obtained by repeatedly performing the following operation zero or more times, modulo 998244353:

Choose an integer i such that 1\leq i\leq |S|-1 and the i-th and (i+1)-th characters in S are different. Replace each of the i-th and (i+1)-th characters in S with the character that differs from both of them (among a, b and c).

Constraints

2 \leq |S| \leq 2 × 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of strings that can be possibly obtained by repeatedly performing the operation, modulo 998244353.

Sample Input 1

abc

Sample Output 1

3

abc, aaa and ccc can be obtained.

Sample Input 2

abbac

Sample Output 2

65

Sample Input 3

babacabac

Sample Output 3

6310

Sample Input 4

ababacbcacbacacbcbbcbbacbaccacbacbacba

Sample Output 4

148010497

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <string.h>

#define N	200000
#define MD	998244353

int main() {
	static char cc[N + 1];
	static int dp[3][3][3][2], dq[3][3][3][2];
	int n, i, ka, kb, kc, ba, ca, a, eq, ans;

	scanf("%s", cc), n = strlen(cc);
	ka = kb = kc = 0;
	for (i = 0; i < n; i++)
		if (cc[i] == 'a')
			ka++;
		else if (cc[i] == 'b')
			kb++;
		else
			kc++;
	if (ka == n || kb == n || kc == n) {
		printf("1\n");
		return 0;
	}
	if (n == 2 && cc[0] != cc[1]) {
		printf("2\n");
		return 0;
	}
	if (n == 3 && cc[0] != cc[1] && cc[1] != cc[2]) {
		printf("3\n");
		return 0;
	}
	kb = (kb - ka) % 3, kc = (kc - ka) % 3;
	if (kb < 0)
		kb += 3;
	if (kc < 0)
		kc += 3;
	dp[2][2][0][0] = 1, dp[1][0][1][0] = 1, dp[0][1][2][0] = 1;
	for (i = 1; i < n; i++) {
		memset(dq, 0, sizeof dq);
		for (ba = 0; ba < 3; ba++)
			for (ca = 0; ca < 3; ca++)
				for (a = 0; a < 3; a++)
					for (eq = 0; eq < 2; eq++) {
						int x = dp[ba][ca][a][eq], a_;

						if (x == 0)
							continue;
						for (a_ = 0; a_ < 3; a_++) {
							int ba_ = ba, ca_ = ca, eq_ = eq || a == a_;

							if (a_ == 0)
								ba_ = (ba_ + 2) % 3, ca_ = (ca_ + 2) % 3;
							else if (a_ == 1)
								ba_ = (ba_ + 1) % 3;
							else
								ca_ = (ca_ + 1) % 3;
							dq[ba_][ca_][a_][eq_] = (dq[ba_][ca_][a_][eq_] + x) % MD;
						}
					}
		memcpy(dp, dq, sizeof dq);
	}
	ans = 1;
	for (i = 1; i < n; i++)
		if (cc[i] == cc[i - 1]) {
			ans = 0;
			break;
		}
	for (a = 0; a < 3; a++)
		ans = (ans + dp[kb][kc][a][1]) % MD;
	printf("%d\n", ans);
	return 0;
}