#include <bits/stdc++.h>
using namespace std;
int n;
int d[51];
long long dp[2][51][51][51][51];
inline void add(long long &a, long long b) { a = (a + b) % 1000000007; }
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> d[i];
  }
  if (d[1] == 3 && d[2] == 3)
    dp[0][0][1][0][1] = 1;
  else if (d[1] == 2 && d[2] == 3)
    dp[0][1][0][0][1] = 1;
  else if (d[1] == 3 && d[2] == 2)
    dp[0][0][1][1][0] = 1;
  else if (d[1] == 2 && d[2] == 2)
    dp[0][1][0][1][0] = 1;
  for (int v = 2; v < n; ++v) {
    int i = v % 2;
    for (int p1 = 0; p1 <= v + 1; ++p1) {
      for (int p2 = 0; p1 + p2 <= v + 1; ++p2) {
        for (int c1 = 0; p1 + p2 + c1 <= v + 1; ++c1) {
          for (int c2 = 0; p1 + p2 + c1 + c2 <= v + 1; ++c2) {
            dp[i ^ 1][p1][p2][c1][c2] = 0;
          }
        }
      }
    }
    for (int p1 = 0; p1 <= v; ++p1) {
      for (int p2 = 0; p1 + p2 <= v; ++p2) {
        for (int c1 = 0; p1 + p2 + c1 <= v; ++c1) {
          for (int c2 = 0; p1 + p2 + c1 + c2 <= v; ++c2) {
            long long curr = dp[i][p1][p2][c1][c2];
            if (d[v + 1] == 3) {
              if (p1 > 0) add(dp[i ^ 1][p1 - 1][p2][c1][c2 + 1], p1 * curr);
              if (p2 > 0) add(dp[i ^ 1][p1 + 1][p2 - 1][c1][c2 + 1], p2 * curr);
              if (p1 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 1 + 1][c2], p1 * c1 * curr);
              if (p2 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 1 + 1][c2], p2 * c1 * curr);
              if (p1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 + 1 + 1][c2 - 1], p1 * c2 * curr);
              if (p2 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 1 + 1][c2 - 1],
                    p2 * c2 * curr);
              if (p1 > 0 && c1 > 1)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 2][c2],
                    p1 * c1 * (c1 - 1) / 2 * curr);
              if (p2 > 0 && c1 > 1)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 2][c2],
                    p2 * c1 * (c1 - 1) / 2 * curr);
              if (p1 > 0 && c2 > 1)
                add(dp[i ^ 1][p1 - 1][p2][c1 + 2][c2 - 2],
                    p1 * c2 * (c2 - 1) / 2 * curr);
              if (p2 > 0 && c2 > 1)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 2][c2 - 2],
                    p2 * c2 * (c2 - 1) / 2 * curr);
              if (p1 > 0 && c1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 1 + 1][c2 - 1],
                    p1 * c1 * c2 * curr);
              if (p2 > 0 && c1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 1 + 1][c2 - 1],
                    p2 * c1 * c2 * curr);
              if (p1 == 0 && p2 == 0 && c1 > 0)
                add(dp[i ^ 1][c1 - 1][c2][0][1], c1 * curr);
              if (p1 == 0 && p2 == 0 && c2 > 0)
                add(dp[i ^ 1][c1 + 1][c2 - 1][0][1], c2 * curr);
            } else {
              if (p1 > 0) add(dp[i ^ 1][p1 - 1][p2][c1 + 1][c2], p1 * curr);
              if (p2 > 0) add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 1][c2], p2 * curr);
              if (p1 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 1][c2], p1 * c1 * curr);
              if (p2 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 1][c2], p2 * c1 * curr);
              if (p1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 + 1][c2 - 1], p1 * c2 * curr);
              if (p2 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 1][c2 - 1], p2 * c2 * curr);
              if (p1 == 0 && p2 == 0 && c1 > 0)
                add(dp[i ^ 1][c1 - 1][c2][1][0], c1 * curr);
              if (p1 == 0 && p2 == 0 && c2 > 0)
                add(dp[i ^ 1][c1 + 1][c2 - 1][1][0], c2 * curr);
            }
          }
        }
      }
    }
  }
  cout << dp[n % 2][0][0][0][0];
}

### Prompt

Generate a CPP solution to the following problem:

Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.

There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts: 

  * Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique; 
  * Let li be the number of roads on the shortest path from town i to the capital, then li ≥ li - 1 holds for all 2 ≤ i ≤ n; 
  * For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3. 



You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 ≤ u, v ≤ n) exists such there is a road between towns u and v in one of them but not in the other.

Input

The first line of input contains a positive integer n (3 ≤ n ≤ 50) — the number of towns.

The second line contains n space-separated integers d1, d2, ..., dn (2 ≤ di ≤ 3) — the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.

Output

Output one integer — the total number of different possible ways in which the towns are connected, modulo 109 + 7.

Examples

Input

4
3 2 3 2


Output

1


Input

5
2 3 3 2 2


Output

2


Input

5
2 2 2 2 2


Output

2


Input

20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2


Output

82944

Note

In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1.

<image>

In the second example, the following two structures satisfy the constraints.

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int d[51];
long long dp[2][51][51][51][51];
inline void add(long long &a, long long b) { a = (a + b) % 1000000007; }
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> d[i];
  }
  if (d[1] == 3 && d[2] == 3)
    dp[0][0][1][0][1] = 1;
  else if (d[1] == 2 && d[2] == 3)
    dp[0][1][0][0][1] = 1;
  else if (d[1] == 3 && d[2] == 2)
    dp[0][0][1][1][0] = 1;
  else if (d[1] == 2 && d[2] == 2)
    dp[0][1][0][1][0] = 1;
  for (int v = 2; v < n; ++v) {
    int i = v % 2;
    for (int p1 = 0; p1 <= v + 1; ++p1) {
      for (int p2 = 0; p1 + p2 <= v + 1; ++p2) {
        for (int c1 = 0; p1 + p2 + c1 <= v + 1; ++c1) {
          for (int c2 = 0; p1 + p2 + c1 + c2 <= v + 1; ++c2) {
            dp[i ^ 1][p1][p2][c1][c2] = 0;
          }
        }
      }
    }
    for (int p1 = 0; p1 <= v; ++p1) {
      for (int p2 = 0; p1 + p2 <= v; ++p2) {
        for (int c1 = 0; p1 + p2 + c1 <= v; ++c1) {
          for (int c2 = 0; p1 + p2 + c1 + c2 <= v; ++c2) {
            long long curr = dp[i][p1][p2][c1][c2];
            if (d[v + 1] == 3) {
              if (p1 > 0) add(dp[i ^ 1][p1 - 1][p2][c1][c2 + 1], p1 * curr);
              if (p2 > 0) add(dp[i ^ 1][p1 + 1][p2 - 1][c1][c2 + 1], p2 * curr);
              if (p1 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 1 + 1][c2], p1 * c1 * curr);
              if (p2 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 1 + 1][c2], p2 * c1 * curr);
              if (p1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 + 1 + 1][c2 - 1], p1 * c2 * curr);
              if (p2 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 1 + 1][c2 - 1],
                    p2 * c2 * curr);
              if (p1 > 0 && c1 > 1)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 2][c2],
                    p1 * c1 * (c1 - 1) / 2 * curr);
              if (p2 > 0 && c1 > 1)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 2][c2],
                    p2 * c1 * (c1 - 1) / 2 * curr);
              if (p1 > 0 && c2 > 1)
                add(dp[i ^ 1][p1 - 1][p2][c1 + 2][c2 - 2],
                    p1 * c2 * (c2 - 1) / 2 * curr);
              if (p2 > 0 && c2 > 1)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 2][c2 - 2],
                    p2 * c2 * (c2 - 1) / 2 * curr);
              if (p1 > 0 && c1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 1 + 1][c2 - 1],
                    p1 * c1 * c2 * curr);
              if (p2 > 0 && c1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 1 + 1][c2 - 1],
                    p2 * c1 * c2 * curr);
              if (p1 == 0 && p2 == 0 && c1 > 0)
                add(dp[i ^ 1][c1 - 1][c2][0][1], c1 * curr);
              if (p1 == 0 && p2 == 0 && c2 > 0)
                add(dp[i ^ 1][c1 + 1][c2 - 1][0][1], c2 * curr);
            } else {
              if (p1 > 0) add(dp[i ^ 1][p1 - 1][p2][c1 + 1][c2], p1 * curr);
              if (p2 > 0) add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 1][c2], p2 * curr);
              if (p1 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 - 1][c2], p1 * c1 * curr);
              if (p2 > 0 && c1 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 - 1][c2], p2 * c1 * curr);
              if (p1 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 - 1][p2][c1 + 1][c2 - 1], p1 * c2 * curr);
              if (p2 > 0 && c2 > 0)
                add(dp[i ^ 1][p1 + 1][p2 - 1][c1 + 1][c2 - 1], p2 * c2 * curr);
              if (p1 == 0 && p2 == 0 && c1 > 0)
                add(dp[i ^ 1][c1 - 1][c2][1][0], c1 * curr);
              if (p1 == 0 && p2 == 0 && c2 > 0)
                add(dp[i ^ 1][c1 + 1][c2 - 1][1][0], c2 * curr);
            }
          }
        }
      }
    }
  }
  cout << dp[n % 2][0][0][0][0];
}

```