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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"

using namespace std;

//const long long int MOD = 1000000007;
//const int MOD = 1000000007;
const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	string s;
	cin >> s;
	if (s.size() == 2) {
		if (s[0] == s[1])cout << 1 << endl;
		else cout << 2 << endl;
		return 0;
	}
	vector<int>v;
	for (auto i : s)v.push_back(i - 'a');
	if (s.size() == 3) {
		set<vector<int>>st;
		st.insert(v);
		for (int i = 0; i < 20; i++) {
			for (auto j : st) {
				for (int k = 0; k < 2; k++) {
					if (j[k] != j[k + 1]) {
						int a = j[k];
						int b = j[k + 1];
						j[k] = 3 - a - b;
						j[k + 1] = 3 - a - b;
						st.insert(j);
						j[k] = a;
						j[k + 1] = b;
					}
				}
			}
		}
		cout << st.size() << endl;
		return 0;
	}
	N = s.size();
	K = accumulate(v.begin(), v.end(), 0) % 3;
	vector<vector<vector<vector<long long int>>>>dp(N, vector<vector<vector<long long int>>>(3, vector<vector<long long int>>(2, vector<long long int>(3))));
	for (int i = 0; i < 3; i++)dp[0][i][0][i] = 1;
	for (int i = 0; i < N - 1; i++) {
		for (int j = 0; j < 3; j++) {
			for (int k = 0; k < 2; k++) {
			//	cout << i << " " << j << " " << k << endl;
				for (int l = 0; l < 3; l++) {
					dp[i][j][k][l] %= MOD;
					for (int m = 0; m < 3; m++) {
						if (j != m) {
							dp[i + 1][m][k][(l + m) % 3] += dp[i][j][k][l];
						}
						else {
							dp[i + 1][m][1][(l + m) % 3] += dp[i][j][k][l];
						}
					}
				}
			}
		}
	}
	long long int ans = 1;
	for (int i = 0; i < 3; i++)ans += dp.back()[i][1][K];
	ans %= MOD;
	for (int i = 1; i < N; i++) {
		if (v[i] == v[i - 1]) {
			ans--;
			break;
		}
	}
	ans += MOD;
	ans %= MOD;
	cout << ans << endl;
	return 0;
}