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: Accepted)

/* https://img.atcoder.jp/arc094/editorial.pdf */
#include <stdio.h>
#include <string.h>

#define N	200000
#define MD	998244353

int main() {
	static char cc[N + 1];
	static int dp[3][3][2], dq[3][3][2];
	int n, i, ka, kb, kc, s, 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] && cc[0] != cc[2]) {
		printf("3\n");
		return 0;
	}
	for (a = 0; a < 3; a++)
		dp[a][a][0] = 1;
	for (i = 1; i < n; i++) {
		memset(dq, 0, sizeof dq);
		for (s = 0; s < 3; s++)
			for (a = 0; a < 3; a++)
				for (eq = 0; eq < 2; eq++) {
					int x = dp[s][a][eq], a_;

					if (x == 0)
						continue;
					for (a_ = 0; a_ < 3; a_++) {
						int s_ = (s + a_) % 3, eq_ = eq || a == a_;

						dq[s_][a_][eq_] = (dq[s_][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;
		}
	s = (kb + kc * 2) % 3;
	for (a = 0; a < 3; a++)
		ans = (ans + dp[s][a][1]) % MD;
	printf("%d\n", ans);
	return 0;
}