Language: C++

#include<bits/stdc++.h>

#define MAXN 200005
#define eps 1e-7
#define mo 974711
#define pb push_back
#define mp make_pair
using namespace std;

typedef long long int64;
typedef unsigned int u32;
typedef double db;

const int64 MOD = 998244353;

int N;
char s[MAXN];

int64 dp[MAXN][3][3][2];

void inc(int64 &x,int64 y) {
    x = x + y;
    while(x >= MOD) 
        x -= MOD;
}

void build(int p,int l,int r){
    if(l>=r){
        tree[p]=a[r];
        return ;
    }
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    tree[p]=tree[lson]+tree[rson];
}

void add(int p,int l,int r,int x,int key){
    if(l>=r){
        tree[p]+=key;
        return ;
    }
    int mid=(l+r)>>1;
    if(x<=mid)
        add(lson,l,mid,x,key);else
        add(rson,mid+1,r,x,key);
    tree[p]=tree[lson]+tree[rson];
}

int query(int p,int l,int r,int sl,int sr){
    if(sl<=l&&r<=sr){
        return tree[p];
    }
    int mid=(l+r)>>1;
    int q=0;
    if(mid>=sl)
        q+=query(lson,l,mid,sl,mid);else
        q+=query(rson,mid+1,r,mid+1,r);
    return q;
}


int main() {
    scanf("%s",s + 1);
    N = strlen(s + 1);
    bool all_same = 1;
    for(int i = 1 ; i <= N ; ++i) {
    	if(s[i] != s[1]) all_same = 0;
    }
    if(all_same) {puts("1");return 0;}
    if(N > 3) {
    	for(int i = 0 ; i <= 2 ; ++i) dp[1][i][i][0] = 1;
    	for(int i = 2 ; i <= N ; ++i) {
    	    for(int s = 0 ; s <= 2 ; ++s) {
    		for(int j = 0 ; j <= 2 ; ++j) {
    		    for(int h = 0 ; h <= 2 ; ++h) {
    			for(int l = 0 ; l <= 1 ; ++l) {
    			    inc(dp[i][(s + h) % 3][h][l || h == j],dp[i - 1][s][j][l]);
    			}
    		    }
    		}
    	    }
    	}
    	bool flag = 1;
    	int c = 0;
    	for(int i = 1 ; i <= N ; ++i) {
    	    c = (c + s[i] - 'a') % 3;
    	}
    	for(int i = 2 ; i <= N ; ++i) {
    	    if(s[i] == s[i - 1]) {flag = 0;break;}
    	}
    	int64 ans = flag;
    	for(int i = 0 ; i <= 2 ; ++i) ans += dp[N][c][i][1];
    	ans %= MOD;
    	printf("%lld\n",ans);
    }
    else {
    	if(N == 3) {
    	    if(s[1] != s[2] && s[2] != s[3] && s[1] != s[3]) puts("3");
    	    else if(s[1] == s[2] || s[2] == s[3]) puts("6");
    	    else puts("7");
    	}
    	else if(N == 2) {
    	    puts("2");
    	}
    }
    return 0;
}

Compiler output:
a.cc: In function 'void build(int, int, int)':
a.cc:29:9: error: 'tree' was not declared in this scope; did you mean 'free'?
   29 |         tree[p]=a[r];
      |         ^~~~
      |         free
a.cc:29:17: error: 'a' was not declared in this scope
   29 |         tree[p]=a[r];
      |                 ^
a.cc:33:11: error: 'lson' was not declared in this scope
   33 |     build(lson,l,mid);
      |           ^~~~
a.cc:34:11: error: 'rson' was not declared in this scope
   34 |     build(rson,mid+1,r);
      |           ^~~~
a.cc:35:5: error: 'tree' was not declared in this scope; did you mean 'free'?
   35 |     tree[p]=tree[lson]+tree[rson];
      |     ^~~~
      |     free
a.cc: In function 'void add(int, int, int, int, int)':
a.cc:40:9: error: 'tree' was not declared in this scope; did you mean 'free'?
   40 |         tree[p]+=key;
      |         ^~~~
      |         free
a.cc:45:13: error: 'lson' was not declared in this scope
   45 |         add(lson,l,mid,x,key);else
      |             ^~~~
a.cc:46:13: error: 'rson' was not declared in this scope
   46 |         add(rson,mid+1,r,x,key);
      |             ^~~~
a.cc:47:5: error: 'tree' was not declared in this scope; did you mean 'free'?
   47 |     tree[p]=tree[lson]+tree[rson];
      |     ^~~~
      |     free
a.cc:47:18: error: 'lson' was not declared in this scope
   47 |     tree[p]=tree[lson]+tree[rson];
      |                  ^~~~
a.cc:47:29: error: 'rson' was not declared in this scope
   47 |     tree[p]=tree[lson]+tree[rson];
      |                             ^~~~
a.cc: In function 'int query(int, int, int, int, int)':
a.cc:52:16: error: 'tree' was not declared in this scope; did you mean 'free'?
   52 |         return tree[p];
      |                ^~~~
      |                free
a.cc:57:18: error: 'lson' was not declared in this scope
   57 |         q+=query(lson,l,mid,sl,mid);else
      |                  ^~~~
a.cc:58:18: error: 'rson' was not declared in this scope
   58 |         q+=query(rson,mid+1,r,mid+1,r);
      |                  ^~~~