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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.Scanner;

public class Main {
  static Scanner sc = new Scanner(System.in);
  static final long MOD = 998244353;

  public static void main(String[] args) {
    char[] S = sc.next().toCharArray();
    int N = S.length;
    boolean all = true;
    boolean any = false;
    int sum = S[N - 1] - '0';
    for (int i = 0; i < N - 1; i++) {
      if (S[i] != S[i + 1]) {
        all = false;
      } else {
        any = true;
      }
      sum += S[i] - '0';
    }
    sum %= 3;
    if (all) {
      System.out.println(1);
      return;
    }
    if (N == 2) {
      System.out.println(2);
      return;
    }
    if (N == 3 && !any && S[0] != S[2]) {
      System.out.println(3);
      return;
    }
    long[][][] dp = new long[3][3][N];
    for (int i = 0; i < 3; i++) {
      dp[i][i][0] = 1;
    }
    for (int i = 0; i < N - 1; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 3; l++) {
            if (l == k) continue;
            dp[(j + l) % 3][l][i + 1] += dp[j][k][i];
            if (dp[(j + l) % 3][l][i + 1] >= MOD) dp[(j + l) % 3][l][i + 1] -= MOD;
          }
        }
      }
    }
    long ans = 1;
    for (int i = 0; i < N - 1; i++) {
      ans *= 3;
      ans %= MOD;
    }
    for (int i = 0; i < 3; i++) {
      ans += MOD - dp[sum][i][N - 1];
    }
    if (!any) ++ans;
    System.out.println(ans % MOD);
  }

}