#include <bits/stdc++.h>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define mod 1000000007
using namespace std;
typedef long long ll;
ll dp[505][505];
bool bio[505][505];
int niz[505];
int pref[505];
int n;
ll add(ll x, ll y){
    return (x+y)%mod;
}
ll mul(ll x, ll y){
    return (x*y)%mod;
}
ll resi(int i, int j){
   // cout << i << " " << j << endl;
    if(i == j+1)return 1;
    int len = j-i+1;
    if(len%4 != 0)return 0;
    if(len == 4){
        int pom0 = 0;
        int pom1 = 0;
        for(int x=i; x<=j; x++){
            if(niz[x] == 0)pom0++;
            else pom1++;
        }
        if(pom0 * pom1 != 3)return 0;
        return 1;
    }
    int kolko = pref[j] - pref[i-1];
    int ost = len - kolko;
    if(3*ost < kolko || 3*kolko < ost)return 0;

    if(bio[i][j])return dp[i][j];
    bio[i][j] = 1;
    dp[i][j] = 0;
    ll res = 0;
    for(int x1=i+1; x1<=j; x1+=4){
        for(int x2=x1+1; x2<=j;x2+=4){
            for(int x3=x2+1; x3<=j; x3+=4){
                int lastgr = j;
                int pom0=0,pom1=0;
                if(niz[x1] == 0)pom0++;
                else pom1++;
                if(niz[x2] == 0)pom0++;
                else pom1++;
                if(niz[x3] == 0)pom0++;
                else pom1++;
                if(niz[i] == 0)pom0++;
                else pom1++;
                if(pom1*pom0 != 3)continue;
                if(lastgr == 0)lastgr = n;
                ll pom = mul(resi(i+1,x1-1),resi(x1+1,x2-1));
                ll pom2 = mul(resi(x2+1,x3-1),resi(x3+1,lastgr));
                pom = mul(pom,pom2);
                dp[i][j] = add(dp[i][j], pom);
            }
        }
    }
    return dp[i][j];
}
int Zvezda(int N, int* A) {
    n = N;
    for(int i=1; i<=n; i++)niz[i] = A[i];
    for(int i=1; i<=n; i++){
        pref[i] = pref[i-1];
        if(niz[i] == 0)pref[i]++;
    }
	return resi(1,n);
}