fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13.  
  14.  
  15. inline int power(int a, int b, int mod) {
  16. int x = 1;
  17. a %= mod;
  18. while (b) {
  19. if (b & 1) x = (x * a) % mod;
  20. a = (a * a) % mod;
  21. b >>= 1;
  22. }
  23. return x;
  24. }
  25.  
  26.  
  27.  
  28. vector<int> fact(N);
  29. vector<int> ifact(N);
  30.  
  31.  
  32. void computeFactorial(int mod){
  33. fact[0] = ifact[0] = 1;
  34. for(int i=1; i<N; i++){
  35. fact[i] = (fact[i-1] * i)% mod;
  36. }
  37.  
  38.  
  39. ifact[N-1] = power(fact[N-1], mod-2, mod);
  40. for(int i = N-2; i>=1; i--){
  41. ifact[i] = (ifact[i+1] * (i+1))%mod;
  42. }
  43. }
  44.  
  45. int nCr(int n, int r, int mod){
  46. if(r>n || r<0) return 0;
  47. return ( ( fact[n] * ifact[r] )%mod * ifact[n-r] ) % mod;
  48. }
  49.  
  50.  
  51.  
  52. //_ ***************************** START Below *******************************
  53.  
  54.  
  55. int MOD = 998244353;
  56.  
  57.  
  58. vector<int> a;
  59.  
  60. int consistency(int n){
  61.  
  62. computeFactorial(MOD);
  63.  
  64. int ways = 1;
  65. for(int i=0; i<=n-3; i+=3){
  66. vector<int> triad = {a[i], a[i+1], a[i+2]};
  67. sort(begin(triad), end(triad), greater<int>());
  68.  
  69. if(triad[0] == triad[2]){
  70. ways = (ways*3)%MOD;
  71. }
  72. else if(triad[0] != triad[1] && triad[1] == triad[2] ){
  73. ways = (ways*2)%MOD;
  74. }
  75. else ways *= 1;
  76. }
  77.  
  78. int t = n/3; // total triads t
  79. int totalWays = (nCr(t, t/2, MOD) * ways)%MOD;
  80.  
  81.  
  82. return totalWays;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. int practice(int n){
  100.  
  101.  
  102. return 0;
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. void solve() {
  110.  
  111. int n;
  112. cin>> n;
  113.  
  114. a.resize(n);
  115. for(int i=0; i<n; i++) cin >> a[i];
  116.  
  117. cout << consistency(n) << endl;
  118.  
  119.  
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126. int32_t main() {
  127. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  128.  
  129. int t = 1;
  130. // cin >> t;
  131. while (t--) {
  132. solve();
  133. }
  134.  
  135. return 0;
  136. }
Success #stdin #stdout 0.01s 7760KB
stdin
12
1 3 3 7 8 5 2 2 2 2 4 2
stdout
36