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 Rust (1.15.1) (judge verdict: Wrong Answer)

/// Thank you tanakh!!!
/// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

use std::collections::{BTreeSet, VecDeque};

const MOD: usize = 998244353;

fn brute_force(s: &Vec<char>) -> usize {
    let n = s.len();
    let mut result = BTreeSet::new();
    result.insert(s.clone());
    let mut q = VecDeque::new();
    q.push_back(s.clone());
    while let Some(s) = q.pop_front() {
        for i in 1..n {
            if s[i] != s[i - 1] {
                let mut s = s.clone();
                for c in 0..3 {
                    let c = ('a' as u8 + c as u8) as char;
                    if c != s[i] && c != s[i - 1] {
                        s[i] = c;
                        s[i - 1] = c;
                        break;
                    }
                }
                if !result.contains(&s) {
                    result.insert(s.clone());
                    q.push_back(s);
                }
            }
        }
    }
    result.len()
}

fn main() {
    input!(s: chars);
    let n = s.len();
    if n <= 3 {
        println!("{}", brute_force(&s));
        return;
    }

    let mut dp = vec![vec![vec![vec![0; 2]; 3]; 3]; n + 1];
    dp[0][0][0][0] = 1;

    for i in 0..n {
        for last_char in 0..3 {
            for remainder in 0..3 {
                for already in 0..2 {
                    for next in 0..3 {
                        let next_already = (already == 1) || (last_char == next && i > 0);
                        let next_already = if next_already { 1 } else { 0 };
                        let next_remainder = (remainder + next) % 3;
                        dp[i + 1][next][next_remainder][next_already] +=
                            dp[i][last_char][remainder][already];
                        dp[i + 1][next][next_remainder][next_already] %= MOD;
                    }
                }
            }
        }
    }

    let remainder = s.iter().map(|&c| c as usize - 'a' as usize).sum::<usize>() % 3;

    let mut ans = 0;
    for last in 0..3 {
        ans += dp[n][last][remainder][1];
    }

    let mut already = false;
    for i in 1..n {
        if s[i - 1] == s[i] {
            already = true;
        }
    }
    if !already {
        ans += 1;
    }
    ans %= MOD;

    println!("{}", ans);
}