func_code_string: #include <bits/stdc++.h>   using namespace std;   bool debug = 0;   int n, m, k;   int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};   string direc =  RDLU ;   long long ln, lk, lm;   void etp(bool f = 0) {     puts(f ?  Yes  :  No );     exit(0);   }   void addmod(int &x, int y, int mod = 1000000007) {     assert(y >= 0);     x += y;     if (x >= mod) x -= mod;     assert(x >= 0 && x < mod);   }   void et() {     puts( -1 );     exit(0);   }   long long fastPow(long long x, long long y, int mod = 1000000007) {     long long ans = 1;     while (y > 0) {       if (y & 1) ans = (x * ans) % mod;       x = x * x % mod;       y >>= 1;     }     return ans;   }   long long gcd1(long long x, long long y) {     long long z = y;     while (x % y != 0) {       z = x % y;       x = y;       y = z;     }     return z;   }   int fac[105], inv[105];   void initfac(int n) {     fac[0] = inv[0] = 1;     for (int(i) = 1; (i) <= (int)(n); (i)++) {       fac[i] = (long long)fac[i - 1] * i % 1000000007;       inv[i] = fastPow(fac[i], 1000000007 - 2);     }   }   int C(int n, int m) {     if (n < m) return 0;     return (long long)fac[n] * inv[m] % 1000000007 * inv[n - m] % 1000000007;   }   int d[105];   long long dp[55][55][55], ways[55][55][55];   long long wk(int a, int b, int c) {     if (a < 0 || b < 0) return 0;     if (dp[a][b][c] != -1) return dp[a][b][c];     int ans = 0;     if (c) {       ans = (wk(a - 1, b, c - 1) * a + wk(a + 1, b - 1, c - 1) * b) % 1000000007;     } else if (a) {       ans = (wk(a - 2, b, 0) * (a - 1) + wk(a, b - 1, 0) * b) % 1000000007;     } else if (b > 2) {       for (long long z = 3, x = (b - 1) * (b - 2) / 2; z <= b;            x = x * (b - z++) % 1000000007) {         ans += x * wk(0, b - z, 0) % 1000000007;         ans %= 1000000007;       }     } else       ans = (b == 0);     return dp[a][b][c] = ans;     ;   }   int mdp[55][55];   int cal(int l, int r) {     if (mdp[l][r] != -1) return mdp[l][r];     if (r > n) return 0;     int ans = 0, c2 = 0, c3 = 0;     for (int i = l; i <= r; i++)       if (d[i] == 2)         c2++;       else         c3++;     if (r == n)       ans = wk(c2, c3, 0) % 1000000007;     else {       for (int x = 1; x <= n - r; x++) {         addmod(ans, wk(c2, c3, x) * cal(r + 1, r + x) % 1000000007);       }     }     return mdp[l][r] = ans;   }   void fmain(int tid) {     scanf( %d , &n);     for (int(i) = 1; (i) <= (int)(n); (i)++) scanf( %d , d + i);     initfac(55);     ways[0][0][0] = 1;     memset(dp, -1, sizeof dp);     memset(mdp, -1, sizeof mdp);     int ans = d[1] == 2 ? cal(2, 3) : cal(2, 4);     printf( %d n , ans);   }   int main() {     int t = 1;     for (int(i) = 1; (i) <= (int)(t); (i)++) {       fmain(i);     }     return 0;   }   