func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MN = 52;   const long long mod = 1e9 + 7;   int N;   long long dp[2][MN][MN][MN][MN];   vector<int> d;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> N;     d.resize(N);     for (int i = 0; i < N; i++) {       cin >> d[i];     }     int cur = 0, next = 1;     dp[cur][d[0] == 2][d[0] == 3][d[1] == 2][d[1] == 3] = 1;     for (int i = 1; i < N - 1; i++) {       memset(dp[next], 0, sizeof(dp[next]));       for (int p1 = 0; p1 <= N; p1++)         for (int p2 = 0; p1 + p2 <= N; p2++)           for (int c1 = 0; c1 + p1 + p2 <= N; c1++)             for (int c2 = 0; c1 + c2 + p1 + p2 <= N; c2++) {               if (dp[cur][p1][p2][c1][c2] == 0) continue;               long long val = dp[cur][p1][p2][c1][c2];               if (p1 == 0 && p2 == 0) {                 dp[cur][c1][c2][0][0] += val;                 dp[cur][c1][c2][0][0] %= mod;                 continue;               }               for (int type = 0; type <= 1; type++) {                 int way;                 if (type == 0) {                   way = p1;                   if (p1 == 0) continue;                   p1--;                 } else {                   way = p2;                   if (p2 == 0) continue;                   p1++, p2--;                 }                 if (d[i + 1] == 2) {                   dp[next][p1][p2][c1 + 1][c2] += val * way;                   dp[next][p1][p2][c1 + 1][c2] %= mod;                   if (c1 >= 1) {                     dp[next][p1][p2][c1 - 1][c2] += val * way * c1;                     dp[next][p1][p2][c1 - 1][c2] %= mod;                   }                   if (c2 >= 1) {                     dp[next][p1][p2][c1 + 1][c2 - 1] += val * way * c2;                     dp[next][p1][p2][c1 + 1][c2 - 1] %= mod;                   }                 } else {                   dp[next][p1][p2][c1][c2 + 1] += val * way;                   dp[next][p1][p2][c1][c2 + 1] %= mod;                   if (c1 >= 1) {                     dp[next][p1][p2][c1 - 1 + 1][c2] += val * way * c1;                     dp[next][p1][p2][c1 - 1 + 1][c2] %= mod;                   }                   if (c2 >= 1) {                     dp[next][p1][p2][c1 + 2][c2 - 1] += val * way * c2;                     dp[next][p1][p2][c1 + 2][c2 - 1] %= mod;                   }                   if (c1 >= 2) {                     dp[next][p1][p2][c1 - 2][c2] += val * way * c1 * (c1 - 1) / 2;                     dp[next][p1][p2][c1 - 2][c2] %= mod;                   }                   if (c2 >= 2) {                     dp[next][p1][p2][c1 + 2][c2 - 2] +=                         val * way * c2 * (c2 - 1) / 2;                     dp[next][p1][p2][c1 + 2][c2 - 2] %= mod;                   }                   if (c1 >= 1 && c2 >= 1) {                     dp[next][p1][p2][c1 - 1 + 1][c2 - 1] += val * way * c2 * c1;                     dp[next][p1][p2][c1 - 1 + 1][c2 - 1] %= mod;                   }                 }                 if (type == 0) {                   p1++;                 } else {                   p1--, p2++;                 }               }             }       cur ^= 1, next ^= 1;     }     cout << dp[cur][0][0][0][0];     return 0;   }   