Language: C++

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragme GCC target("avx2");
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>

using namespace std;
using namespace __gnu_pbds;

/*
	st.insert(k);
	st.find_by_order(k); iterator on k-th from 0
	st.order_of_key(k); strictly more than k
*/

typedef
tree<
  ll,
  null_type,
  less_equal<ll>,
  rb_tree_tag,
  tree_order_statistics_node_update>
ordered_set;


const int N = 4e5 + 19;
const int M = 2e6 + 19;
const int mod = 998244353;
const int inf = 2e9;
const int blocksz = 300;
const ll INF = 1e18;


inline int modulo(int x) {x %= mod; return (x + mod) % mod; }
inline int mult(int x, int y) { ll x1 = modulo(x), y1 = modulo(y); return (x1 * y1) % mod; }
inline void add(int &x, int y) { x = modulo(x + y); }
inline void Max(int &x, int y) { x = max(x, y); }
inline void Min(int &x, int y) { x = min(x, y); }

ll n;
string s;

//int pw(int x, int y) {
//	if (y == 0)
//		return 1;
//	int res;
//	if (y % 2) {
//		res = pw(x, y - 1);
//		return mult(res, x);
//	} else {
//		res = pw(x, y / 2);
//		return mult(res, res);
//	}
//}
//
//
//int C(int n, int k) {
//	if (n > k)
//		return 0;
//	int t1, t2, t3;
//	t1 = fact[n];
//	t2 = pw(fact[k], mod - 2);
//	t3 = pw(fact[n - k], mod - 2);
//	return mult(t1, mult(t2, t3));
//}


main() {
	ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
	//freopen("input.txt", "r", stdin);
	ll R, G, B, RG, RB, GB, ost, ans = 1;
	R = G = B = RG = RB = GB = 0;
	cin >> n;
	cin >> s;
	ost = n;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == 'R') {
			if (GB) {
				ans = mult(ans, GB);
				GB--;
			} else if (B) {
				ans = mult(ans, B);
				B--;
				RB++;
			} else if (G) {
				ans = mult(ans, G);
				G--;
				RG++;
			} else {
				ans = mult(ans, ost);
				R++;
				ost--;
			}
		} else if (s[i] == 'G') {
			if (RB) {
				ans = mult(ans, RB);
				RB--;
			} else if (R) {
				ans = mult(ans, R);
				R--;
				RG++;
			} else if (B) {
				ans = mult(ans, B);
				B--;
				GB++;
			} else {
				ans = mult(ans, ost);
				G++;
				ost--;
			}
		} else {			
			if (RG) {
				ans = mult(ans, RG);
				RG--;
			} else if (R) {
				ans = mult(ans, R);
				R--;
				RB++;
			} else if (G) {
				ans = mult(ans, G);
				G--;
				RB++;
			} else {
				ans = mult(ans, ost);
				B++;
				ost--;
			}
		}
	}
	cout << ans;	
	return 0;
}
/*
3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2
*/



Compiler output:
/usr/include/c++/14/atomic: In member function 'void std::atomic<_Tp>::store(_Tp, std::memory_order)':
a.cc:11:13: error: expected primary-expression before 'long'
   11 | #define int long long
      |             ^~~~
/usr/include/c++/14/atomic: In member function 'void std::atomic<_Tp>::store(_Tp, std::memory_order) volatile':
a.cc:11:13: error: expected primary-expression before 'long'
   11 | #define int long long
      |             ^~~~
/usr/include/c++/14/atomic: In member function '_Tp std::atomic<_Tp>::load(std::memory_order) const':
a.cc:11:13: error: expected primary-expression before 'long'
   11 | #define int long long
      |             ^~~~
/usr/include/c++/14/atomic: In member function '_Tp std::atomic<_Tp>::load(std::memory_order) const volatile':
a.cc:11:13: error: expected primary-expression before 'long'
   11 | #define int long long
      |             ^~~~
/usr/include/c++/14/atomic: In member function '_Tp std::atomic<_Tp>::exchange(_Tp, std::memory_order)':
a.cc:11:13: error: expected primary-expression before 'long'
   11 | #define int long long
      |             ^~~~
/usr/include/c++/14/atomic: In member function '_Tp std::atomic<_Tp>::exchange(_Tp, std::memory_order) volatile':
a.cc:11:13: error: expected primary-expression before 'long'
   11 | #define int long long
      |             ^~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:64,
                 from a.cc:12:
/usr/include/c++/14/atomic: At global scope:
/usr/include/c++/14/atomic:919:12: error: redefinition of 'struct std::atomic<long long int>'
  919 |     struct atomic<long long> : __atomic_base<long long>
      |            ^~~~~~~~~~~~~~~~~
/usr/include/c++/14/atomic:827:12: note: previous definition of 'struct std::atomic<long long int>'
  827 |     struct atomic<int> : __atomic_base<int>
      |            ^~~~~~~~~~~
/usr/include/c++/14/atomic:942:12: error: redefinition of 'struct std::atomic<long long unsigned int>'
  942 |     struct atomic<unsigned long long> : __atomic_base<unsigned long long>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/atomic:850:12: note: previous definition of 'struct std::atomic<long long unsigned int>'
  850 |     struct atomic<unsigned int> : __atomic_base<unsigned int>
      |            ^~~~~~~~~~~~~~~~~~~~
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:13: error: 'long long long' is too long for GCC
   11 | #define int long long
      |             ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:13: error: 'long long long' is too long for GCC
   11 | #define int long long
      |             ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:13: error: 'long long long' is too long for GCC
   11 | #define int long long
      |             ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
a.cc:11:18: error: 'long long long' is too long for GCC
   11 | #define int long long
      |                  ^~~~
In file included from /usr/include/c++/14/bits/basic_ios.h:37,
                 from /usr/include/c++/14/ios:46,
                 from /usr/include/c++/14/istream:40,
                 from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/bits/locale_facets.h:2072:7: error: 'std::num_get<_CharT, _InIter>::iter_type std::num_get<_CharT, _InIter>::get(iter_type, iter_type, std::ios_base&, std::ios_base::iostate&, long long unsigned int&) const' cannot be overloaded with 'std::num_get<_CharT, _InIter>::iter_type std::num_get<_CharT, _InIter>::get(iter_type, iter_type, std::ios_base&, std::ios_base::iostate&, long long unsigned int&) const'
 2072 |       get(iter_type __in, iter_type __end, ios_base& __io,
      |       ^~~
/usr/include/c++/14/bits/locale_facets.h:2054:7: note: previous declaration 'std::num_get<_CharT, _InIter>::iter_type std::num_get<_CharT, _InIter>::get(iter_type, iter_type, std::ios_base&, std::ios_base::iostate&, long long unsigned int&) const'
 2054 |       get(iter_type __in, iter_type __end, ios_base& __io,
      |       ^~~
/usr/include/c++/14/bits/locale_facets.h:2254:7: error: 'virtual std::num_get<_CharT, _InIter>::iter_type std::num_get<_CharT, _InIter>::do_get(iter_type, iter_type, std::ios_base&, std::ios_base::iostate&, long long unsigned int&) const' cannot be overloaded with 'virtual std::num_get<_CharT, _InIter>::iter_type std::num_get<_CharT, _InIter>::do_get(iter_type, iter_type, std::ios_base&, std::ios_base::iostate&, long long unsigned int&) const'
 2254 |       do_get(iter_type __beg, iter_type __end, ios_base& __io,
      |       ^~~~~~
/usr/include/c++/14/bits/locale_facets.h:2236:7: note: previous declaration 'virtual std::num_get<_CharT, _InIter>::iter_type std::num_get<_CharT, _InIter>::do_get(iter_type, iter_type, std::ios_base&, std::ios_base::iostate&, long long unsigned int&) const'
 2236 |       do_get(iter_type __beg, iter_type __end, ios_base& __io,
      |       ^~~~~~
In file included from /usr/include/c++/14/istream:41:
/usr/include/c++/14/ostream:211:7: error: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int)' cannot be overloaded with 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int)'
  211 |       operator<<(long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:197:7: note: previous declaration 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int)'
  197 |       operator<<(int __n);
      |       ^~~~~~~~
/usr/include/c++/14/ostream:215:7: error: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int)' cannot be overloaded with 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int)'
  215 |       operator<<(unsigned long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:200:7: note: previous declaration 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int)'
  200 |       operator<<(unsigned int __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:199:7: error: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&)' cannot be overloaded with 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&)'
  199 |       operator>>(long long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:181:7: note: previous declaration 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&)'
  181 |       operator>>(int& __n);
      |       ^~~~~~~~
/usr/include/c++/14/istream:203:7: error: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&)' cannot be overloaded with 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&)'
  203 |       operator>>(unsigned long long& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:184:7: note: previous declaration 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&)'
  184 |       operator>>(unsigned int& __n)
      |       ^~~~~~~~
/usr/include/c++/14/bits/istream.tcc: In member function 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&)':
a.cc:11:13: error: expected primary-expression before 'long'
   11 | #define int long long
      |             ^~~~
/usr/include/x86_64-linux-gnu/bits/fenv.h: At global scope:
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:13: error: 'long' and 'short' specified together
   11 | #define int long long
      |             ^~~~
a.cc:11:13: