Problem p02940 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
template <class T> void read(T &x) {
  x = 0;
  int f = 1, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 - '0' + ch;
    ch = getchar();
  }
  x *= f;
}
typedef long long ll;
const int mod = 998244353;
const int maxn = 1e5 + 10;
int n;
char s[maxn];
int main() {
  read(n);
  scanf("%s", s + 1);
  int an = 1;
  int t = n, r = 0, b = 0, g = 0, rb = 0, rg = 0, bg = 0;
  for (int i = 1; i <= n * 3; ++i) {
    if (s[i] == 'R') {
      if (bg) {
        an = (ll)an * bg % mod;
        --bg;
      } else if (b) {
        an = (ll)an * b % mod;
        --b, ++rb;
      } else if (g) {
        an = (ll)an * g % mod;
        --g, ++rg;
      } else {
        an = (ll)an * t % mod;
        --t, ++r;
      }
    } else if (s[i] == 'B') {
      if (rg) {
        an = (ll)an * rg % mod;
        --rg;
      } else if (r) {
        an = (ll)an * r % mod;
        --r, ++rb;
      } else if (g) {
        an = (ll)an * g % mod;
        --g, ++bg;
      } else {
        an = (ll)an * t % mod;
        --t, ++b;
      }
    } else if (s[i] == 'G') {
      if (rb) {
        an = (ll)an * rb % mod;
        --rb;
      } else if (b) {
        an = (ll)an * b % mod;
        --b, ++bg;
      } else if (r) {
        an = (ll)an * r % mod;
        --r, ++rg;
      } else {
        an = (ll)an * t % mod;
        --t, ++g;
      }
    }
  }
  printf("%d\n", an);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
template <class T> void read(T &x) {
  x = 0;
  int f = 1, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 - '0' + ch;
    ch = getchar();
  }
  x *= f;
}
typedef long long ll;
const int mod = 998244353;
const int maxn = 1e5 + 10;
int n;
char s[maxn * 3];
int main() {
  read(n);
  scanf("%s", s + 1);
  int an = 1;
  int t = n, r = 0, b = 0, g = 0, rb = 0, rg = 0, bg = 0;
  for (int i = 1; i <= n * 3; ++i) {
    if (s[i] == 'R') {
      if (bg) {
        an = (ll)an * bg % mod;
        --bg;
      } else if (b) {
        an = (ll)an * b % mod;
        --b, ++rb;
      } else if (g) {
        an = (ll)an * g % mod;
        --g, ++rg;
      } else {
        an = (ll)an * t % mod;
        --t, ++r;
      }
    } else if (s[i] == 'B') {
      if (rg) {
        an = (ll)an * rg % mod;
        --rg;
      } else if (r) {
        an = (ll)an * r % mod;
        --r, ++rb;
      } else if (g) {
        an = (ll)an * g % mod;
        --g, ++bg;
      } else {
        an = (ll)an * t % mod;
        --t, ++b;
      }
    } else if (s[i] == 'G') {
      if (rb) {
        an = (ll)an * rb % mod;
        --rb;
      } else if (b) {
        an = (ll)an * b % mod;
        --b, ++bg;
      } else if (r) {
        an = (ll)an * r % mod;
        --r, ++rg;
      } else {
        an = (ll)an * t % mod;
        --t, ++g;
      }
    }
  }
  printf("%d\n", an);
  return 0;
}