func_code_string: #include <bits/stdc++.h>   using namespace std;   const long long INF = 2e9 + 7;   const int M = 1e6 + 10;   const int N = 51;   const int POW = 20;   const long long MOD = 1000 * 1000 * 1000 + 7;   int a[N];   int d[2][N][N][N][N];   void add(int &a, long long add) { a = ((long long)a + add) % MOD; }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     if (a[0] == 3) {       if (a[1] == 2)         d[1][0][1][1][0] = 1;       else         d[1][0][1][0][1] = 1;     } else {       d[0][0][0][0][1] = 1;     }     for (int i = a[0] - 2; i < n - 1; ++i) {       for (int s1 = 0; s1 < n + 1; ++s1)         for (int s2 = 0; s2 < n + 1; ++s2)           for (int t1 = 0; t1 < n + 1; ++t1)             for (int t2 = 0; t2 < n + 1; ++t2) d[(i + 1) % 2][s1][s2][t1][t2] = 0;       for (int s1 = 0; s1 < n + 1; ++s1)         for (int s2 = 0; s2 < n + 1; ++s2)           for (int t1 = 0; t1 < n + 1; ++t1)             for (int t2 = 0; t2 < n + 1; ++t2) {               if (d[i % 2][s1][s2][t1][t2] == 0) continue;               assert(s1 < n && s2 < n && t1 < n && t2 < n);               long long cur = d[i % 2][s1][s2][t1][t2];               if (s1 + s2 == 0) {                 if (t1)                   add(d[(i + 1) % 2][t1 - 1][t2][a[i + 1] == 2][a[i + 1] == 3],                       cur * t1);                 if (t2)                   add(d[(i + 1) % 2][t1 + 1][t2 - 1][a[i + 1] == 2]                        [a[i + 1] == 3],                       cur * t2);               }               if (s1) {                 add(d[(i + 1) % 2][s1 - 1][s2][t1 + (a[i + 1] == 2)]                      [t2 + (a[i + 1] == 3)],                     cur * s1);                 if (a[i + 1] == 2) {                   if (t1)                     add(d[(i + 1) % 2][s1 - 1][s2][t1 - 1][t2], cur * s1 * t1);                   if (t2)                     add(d[(i + 1) % 2][s1 - 1][s2][t1 + 1][t2 - 1],                         cur * s1 * t2);                 } else if (a[i + 1] == 3) {                   if (t1) add(d[(i + 1) % 2][s1 - 1][s2][t1][t2], cur * s1 * t1);                   if (t1 >= 2)                     add(d[(i + 1) % 2][s1 - 1][s2][t1 - 2][t2],                         cur * s1 * (t1 * (t1 - 1) / 2));                   if (t2 >= 2)                     add(d[(i + 1) % 2][s1 - 1][s2][t1 + 2][t2 - 2],                         cur * s1 * (t2 * (t2 - 1) / 2));                   if (t2)                     add(d[(i + 1) % 2][s1 - 1][s2][t1 + 2][t2 - 1],                         cur * s1 * t2);                   if (t1 && t2)                     add(d[(i + 1) % 2][s1 - 1][s2][t1][t2 - 1],                         cur * s1 * t1 * t2);                 }               }               if (s2) {                 add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1 + (int)(a[i + 1] == 2)]                      [t2 + (int)(a[i + 1] == 3)],                     cur * s2);                 if (a[i + 1] == 2) {                   if (t1)                     add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1 - 1][t2],                         cur * s2 * t1);                   if (t2)                     add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1 + 1][t2 - 1],                         cur * s2 * t2);                 } else if (a[i + 1] == 3) {                   if (t1)                     add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1][t2], cur * s2 * t1);                   if (t1 >= 2)                     add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1 - 2][t2],                         cur * s2 * (t1 * (t1 - 1) / 2));                   if (t2 >= 2)                     add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1 + 2][t2 - 2],                         cur * s2 * (t2 * (t2 - 1) / 2));                   if (t2)                     add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1 + 2][t2 - 1],                         cur * s2 * t2);                   if (t1 && t2)                     add(d[(i + 1) % 2][s1 + 1][s2 - 1][t1][t2 - 1],                         cur * s2 * t1 * t2);                 }               }             }     }     cout << d[(n + 1) % 2][0][0][0][0];     return 0;   }   