Problem p03392 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD = 998244353;
int O = 332748118;
int dp[300000][3];
signed main() {
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  int sz = s.size();
  dp[0][0] = 1, dp[0][1] = 1, dp[0][2] = 1;
  for (int i = 1; i < sz; i++) {
    dp[i][0] = (dp[i - 1][1] + dp[i - 1][2]) % MOD;
    dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
    dp[i][2] = (dp[i - 1][1] + dp[i - 1][0]) % MOD;
  }
  int x = (dp[sz - 1][0] + dp[sz - 1][1] + dp[sz - 1][2]) % MOD;
  int S = 1;
  for (int i = 0; i < sz; i++) {
    S *= 3;
    S %= MOD;
  }
  S -= x;
  S %= MOD;
  if (S < 0)
    S += MOD;
  S *= O;
  S %= MOD;
  bool df = true;
  for (int i = 0; i < s.size() - 1; i++) {
    if (s[i] != s[i + 1])
      df = false;
  }
  if (df) {
    cout << 1 << endl;
    return 0;
  }
  bool eq = false;
  for (int i = 0; i < s.size() - 1; i++) {
    if (s[i] == s[i + 1])
      eq = true;
  }
  if (sz % 3 != 0) {
    cout << (S + !eq) % MOD << endl;
    return 0;
  } else {
    int A = sz / 3 - 1, num = 1;
    while (A > 0) {
      A--;
      num *= 2;
      num %= MOD;
    }
    int aa = 0, bb = 0, cc = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'a')
        aa++;
      if (s[i] == 'b')
        bb++;
      if (s[i] == 'c')
        cc++;
    }
    bool good = true;
    if (aa % 3 == 0 && bb % 3 == 0 && cc % 3 == 0)
      good = false;
    if (aa % 3 != 0 && bb % 3 != 0 && cc % 3 != 0)
      good = false;
    if (good) {
      cout << (S + !eq + num) % MOD << endl;
    } else {
      if (sz == 3) {
        cout << 3 << endl;
        return 0;
      }
      S -= 2 * num;
      S %= MOD;
      if (S < 0)
        S += MOD;
      cout << (S + !eq) % MOD << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD = 998244353;
int O = 332748118;
int dp[300000][3];
signed main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  int sz = s.size();
  dp[0][0] = 1, dp[0][1] = 1, dp[0][2] = 1;
  for (int i = 1; i < sz; i++) {
    dp[i][0] = (dp[i - 1][1] + dp[i - 1][2]) % MOD;
    dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
    dp[i][2] = (dp[i - 1][1] + dp[i - 1][0]) % MOD;
  }
  int x = (dp[sz - 1][0] + dp[sz - 1][1] + dp[sz - 1][2]) % MOD;
  int S = 1;
  for (int i = 0; i < sz; i++) {
    S *= 3;
    S %= MOD;
  }
  S -= x;
  S %= MOD;
  if (S < 0)
    S += MOD;
  S *= O;
  S %= MOD;
  bool df = true;
  for (int i = 0; i < s.size() - 1; i++) {
    if (s[i] != s[i + 1])
      df = false;
  }
  if (df) {
    cout << 1 << endl;
    return 0;
  }
  bool eq = false;
  for (int i = 0; i < s.size() - 1; i++) {
    if (s[i] == s[i + 1])
      eq = true;
  }
  if (sz % 3 != 0) {
    cout << (S + !eq) % MOD << endl;
    return 0;
  } else {
    int A = sz / 3 - 1, num = 1;
    while (A > 0) {
      A--;
      num *= 2;
      num %= MOD;
    }
    int aa = 0, bb = 0, cc = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'a')
        aa++;
      if (s[i] == 'b')
        bb++;
      if (s[i] == 'c')
        cc++;
    }
    bool good = true;
    if (aa % 3 == 0 && bb % 3 == 0 && cc % 3 == 0)
      good = false;
    if (aa % 3 != 0 && bb % 3 != 0 && cc % 3 != 0)
      good = false;
    if (good) {
      cout << (S + !eq + num) % MOD << endl;
    } else {
      if (sz == 3) {
        cout << 3 << endl;
        return 0;
      }
      S -= 2 * num;
      S %= MOD;
      if (S < 0)
        S += MOD;
      cout << (S + !eq) % MOD << endl;
    }
  }
  return 0;
}