#include <bits/stdc++.h>
#define MOD 1000000007
#define MAX_N 505
using namespace std;

long long dp[MAX_N][MAX_N];
int* A;
void calc(int i, int j);
inline int ddp(int i, int j){
    if(dp[i][j]==-1)calc(i, j);
    //printf("ddp(%d, %d)=%d\n", i, j, dp[i][j]);
    return dp[i][j];
}

void dpstep(int i, int j, int* col){
    for(int pt1=i+1; pt1<j; pt1+=4){
        if(A[pt1]!=col[0])continue;
        if(ddp(i+1, pt1)==0)continue;
        for(int pt2=pt1+1; pt2<j; pt2+=4){
            if(A[pt2]!=col[1])continue;
            if(ddp(pt1+1, pt2)==0)continue;
            long long dop=(long long)ddp(i+1, pt1)*(long long)ddp(pt1+1, pt2);
            dop%=MOD;
            long long dop3=0;
            for(int pt3=pt2+1; pt3<j; pt3+=4){
                if(A[pt3]!=col[2])continue;
                dop3+=((long long)ddp(pt2+1, pt3)*(long long)ddp(pt3+1, j))%MOD;
                if(dop3>MOD)dop3%=MOD;
            }
            dop*=dop3;
            dp[i][j]+=dop;
            if(dp[i][j]>MOD)dp[i][j]%=MOD;
        }
    }
}

void calc(int i, int j){
    dp[i][j]=0;
    if(i==j){dp[i][j]=1;return;}
    if(j-i<4){dp[i][j]=0;return;}
    if(A[i]==0){
        //imamo dva slucaja, 0001 i 0111
        //0111
        int col1[3]={1, 1, 1};
        dpstep(i, j, col1);
        int col2[3]={0, 0, 1};
        dpstep(i, j, col2);
        int col3[3]={0, 1, 0};
        dpstep(i, j, col3);
        int col4[3]={1, 0, 0};
        dpstep(i, j, col4);
    }
    else{
        //imamo dva slucaja, 0001 i 0111
        //0111
        int col1[3]={0, 0, 0};
        dpstep(i, j, col1);
        int col2[3]={0, 1, 1};
        dpstep(i, j, col2);
        int col3[3]={1, 1, 0};
        dpstep(i, j, col3);
        int col4[3]={1, 0, 1};
        dpstep(i, j, col4);
    }
}
int num[MAX_N];
int Zvezda(int N, int* a) {
    A=a+1;
    memset(dp, -1, sizeof(dp));
    num[0]=0;
    for(int i=0; i<N; ++i){
        num[i+1]=num[i]+A[i];
    }
    for(int i=0; i<N; ++i){
        for(int j=i+1; j<=N; ++j){
            int n1=num[j]-num[i];
            int n0=j-i-n1;
            if(n1%2!=n0%2)dp[i][j]=0;
            if(n1>3*n0)dp[i][j]=0;
            if(n0>3*n1)dp[i][j]=0;
        }
    }
	return ddp(0, N)%MOD;
}