Problem p02940 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define zerol

using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif

//--------------------------------------------------------------------------------------------

const int MAXN = 1e5 + 5;
const LL MOD = 998244353;
typedef vector<int> vi;

vi r, g, b, low, mid, high;

char s[MAXN];

int n, len;

int main(int argc, char const *argv[]) {
  scanf("%d%s", &n, s);
  // printf("%d %s\n", n, s);
  len = n * 3;
  // dbg(n, len);
  for (int i = 0; i < len; ++i) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else if (s[i] == 'B')
      b.push_back(i);
  }
  low.resize(n);
  high.resize(n);
  mid.resize(n);
  for (int i = 0; i < n; ++i) {
    vi tmp = {r[i], g[i], b[i]};
    sort(tmp.begin(), tmp.end());
    low[i] = tmp[0];
    mid[i] = tmp[1];
    high[i] = tmp[2];
  }

  LL ans = 1;
  for (int i = 1; i <= n; ++i)
    ans = ans * i % MOD;
  int j = 0;
  for (int i = 0; i < n; ++i) {
    while (j + 1 < n && low[j + 1] <= mid[i])
      ++j;
    ans = ans * (j - i + 1) % MOD;
  }
  j = 0;
  for (int i = 0; i < n; ++i) {
    while (j + 1 < n && mid[j + 1] <= high[i])
      ++j;
    ans = ans * (j - i + 1) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define zerol

using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif

//--------------------------------------------------------------------------------------------

const int MAXN = 3e5 + 5;
const LL MOD = 998244353;
typedef vector<int> vi;

vi r, g, b, low, mid, high;

char s[MAXN];

int n, len;

int main(int argc, char const *argv[]) {
  scanf("%d%s", &n, s);
  // printf("%d %s\n", n, s);
  len = n * 3;
  // dbg(n, len);
  for (int i = 0; i < len; ++i) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else if (s[i] == 'B')
      b.push_back(i);
  }
  low.resize(n);
  high.resize(n);
  mid.resize(n);
  for (int i = 0; i < n; ++i) {
    vi tmp = {r[i], g[i], b[i]};
    sort(tmp.begin(), tmp.end());
    low[i] = tmp[0];
    mid[i] = tmp[1];
    high[i] = tmp[2];
  }

  LL ans = 1;
  for (int i = 1; i <= n; ++i)
    ans = ans * i % MOD;
  int j = 0;
  for (int i = 0; i < n; ++i) {
    while (j + 1 < n && low[j + 1] <= mid[i])
      ++j;
    ans = ans * (j - i + 1) % MOD;
  }
  j = 0;
  for (int i = 0; i < n; ++i) {
    while (j + 1 < n && mid[j + 1] <= high[i])
      ++j;
    ans = ans * (j - i + 1) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}