Problem p02940 (C++)

Buggy submission (status: Runtime Error):
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e5, MOD = 998244353;

int n;
char str[N + 3];

struct MODNUM {
  int num;
  MODNUM() {}
  MODNUM(int _n) : num(_n) {}
  MODNUM operator*(const MODNUM B) { return 1ll * num * B.num % MOD; }
  MODNUM operator+(const MODNUM B) { return (1ll * num + B.num) % MOD; }
  MODNUM operator-(const MODNUM B) {
    return ((1ll * num - B.num) % MOD + MOD) % MOD;
  }
  MODNUM operator+=(const MODNUM B) { return *this = (*this) + B; }
  MODNUM operator*=(const MODNUM B) { return *this = (*this) * B; }
  MODNUM operator-=(const MODNUM B) { return *this = (*this) - B; }
} ans, num[4][4];

int main() {
  // freopen("legend.in","r",stdin);
  // freopen("legend.out","w",stdout);
  scanf("%d%s", &n, str + 1);
  for (int i = 1; i <= 3 * n; i++) {
    if (str[i] == 'R')
      str[i] = '1';
    if (str[i] == 'G')
      str[i] = '2';
    if (str[i] == 'B')
      str[i] = '3';
  }
  ans = 1;
  for (int i = 1; i <= 3 * n; i++) {
    int ano1, ano2, thi = str[i] - '0';
    if (str[i] == '1')
      ano1 = 2, ano2 = 3;
    if (str[i] == '2')
      ano1 = 1, ano2 = 3;
    if (str[i] == '3')
      ano1 = 1, ano2 = 2;
    if (num[ano1][ano2].num)
      ans *= num[ano1][ano2], num[ano1][ano2] -= 1;
    else if (num[ano1][0].num)
      ans *= num[ano1][0], num[ano1][0] -= 1,
          num[min(ano1, thi)][max(ano1, thi)] += 1;
    else if (num[ano2][0].num)
      ans *= num[ano2][0], num[ano2][0] -= 1,
          num[min(ano2, thi)][max(ano2, thi)] += 1;
    else
      num[thi][0] += 1;
  }
  for (int i = 1; i <= n; i++)
    ans *= i;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e5, MOD = 998244353;

int n;
char str[3 * N + 3];

struct MODNUM {
  int num;
  MODNUM() {}
  MODNUM(int _n) : num(_n) {}
  MODNUM operator*(const MODNUM B) { return 1ll * num * B.num % MOD; }
  MODNUM operator+(const MODNUM B) { return (1ll * num + B.num) % MOD; }
  MODNUM operator-(const MODNUM B) {
    return ((1ll * num - B.num) % MOD + MOD) % MOD;
  }
  MODNUM operator+=(const MODNUM B) { return *this = (*this) + B; }
  MODNUM operator*=(const MODNUM B) { return *this = (*this) * B; }
  MODNUM operator-=(const MODNUM B) { return *this = (*this) - B; }
} ans, num[4][4];

int main() {
  // freopen("legend.in","r",stdin);
  // freopen("legend.out","w",stdout);
  scanf("%d%s", &n, str + 1);
  for (int i = 1; i <= 3 * n; i++) {
    if (str[i] == 'R')
      str[i] = '1';
    if (str[i] == 'G')
      str[i] = '2';
    if (str[i] == 'B')
      str[i] = '3';
  }
  ans = 1;
  for (int i = 1; i <= 3 * n; i++) {
    int ano1, ano2, thi = str[i] - '0';
    if (str[i] == '1')
      ano1 = 2, ano2 = 3;
    if (str[i] == '2')
      ano1 = 1, ano2 = 3;
    if (str[i] == '3')
      ano1 = 1, ano2 = 2;
    if (num[ano1][ano2].num)
      ans *= num[ano1][ano2], num[ano1][ano2] -= 1;
    else if (num[ano1][0].num)
      ans *= num[ano1][0], num[ano1][0] -= 1,
          num[min(ano1, thi)][max(ano1, thi)] += 1;
    else if (num[ano2][0].num)
      ans *= num[ano2][0], num[ano2][0] -= 1,
          num[min(ano2, thi)][max(ano2, thi)] += 1;
    else
      num[thi][0] += 1;
  }
  for (int i = 1; i <= n; i++)
    ans *= i;
  printf("%d\n", ans);
  return 0;
}