1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   | #include <bits/stdc++.h>
  using namespace std;
  #define ll long long #define ull unsigned long long #define db double #define ldb long double 
  #define fi first #define se second #define MP make_pair #define pii pair <int, int> #define pil pair <int, ll>  #define pli pair <ll, int> #define pll pair <ll, ll>  
  #define All(x) x.begin(), x.end() #define pb push_back #define pf push_front
  #define ms0(x) memset(x, 0, sizeof(x)) #define ms1(x) memset(x, -1, sizeof(x))
  #define oye cerr << "Yes!" << endl;  #define O(x) cerr << #x << ": " << x << endl;  
  template <typename T> void printarr(T a[], int b, int e) {   if(b > e) return ;    for(int i = b; i < e; i++) cout << a[i] << " ";    cout << a[e] << endl;  } 
  template <typename T> int chkmax(T &x, const T &y) {   return x < y ? x = y, 1 : 0;  }
  template <typename T> int chkmin(T &x, const T &y) {   return x > y ? x = y, 1 : 0;  }
  const int N = 1001000;  const int mod = 998244353; 
  int addp(int x, int y) {   return (x += y) >= mod ? x - mod : x;  }
  char s[N];  int n;  int ans = 1; int cnt[3];  
  int main() {   scanf("%d", &n); n *= 3;     scanf("%s", s + 1);   for(int i = 1; i <= n; i++) {     int x[3], y[3];      x[0] = cnt[0]; x[1] = cnt[1], x[2] = cnt[2];      sort(x, x + 3);      int op = 0;      if(s[i] == 'R') op = 0;      if(s[i] == 'G') op = 1;      if(s[i] == 'B') op = 2;      cnt[op]++;      y[0] = cnt[0]; y[1] = cnt[1], y[2] = cnt[2];      sort(y, y + 3);      if(y[2] != x[2]) {       ;      }     else if(y[0] != x[0]) {       ans = 1ll * ans * (x[1] - x[0]) % mod;      }     else {       ans = 1ll * ans * (x[2] - x[1]) % mod;      }   }   for(int i = 1; i <= n / 3; i++) ans = 1ll * ans * i % mod;    printf("%d\n", ans);    return 0;  }
   |