#include <iostream>
using namespace std;

int Zvezda(int N, int *A);

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

long long dp[maxn][maxn][4];
/*
[4] koliko nula je bilo do sada
*/
int b[maxn];

int Zvezda(int n,int a[])
{
    for(int i=0;i<n;i++)b[i]=a[i+1];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            for(int bb=0;bb<4;bb++)
            {
                dp[i][j][bb]=0LL;
            }

    for(int i=0;i<n;i++)
    {
        dp[i][i][0]=(b[i]==0);
        dp[i][i][1]=(b[i]==1);
        dp[i][i][2]=(b[i]==0);
        dp[i][i][3]=(b[i]==1);
    }

    /*for(int d=0;d<n;d++)
    {
        cout<<d<<":  ";
        for(int i=0;i<n;i++)
        {
            int kraj=(i+d)%n;
            for(int br0=0;br0<4;br0++)cout<<dp[i][kraj][br0]<<",";
            cout<<" ";
        }
        cout<<endl;
    }*/

    for(int d=1;d<n;d++)
        for(int i=0;i<n;i++)
        {
            int kraj=(i+d)%n;
            int ukL=4-((d+1)%4);
            if(ukL==4)ukL=0;
            for(int br0=0;br0<=ukL;br0++)
            {
                int br1=ukL-br0;

                int boja=2;
                if(br1==3)boja=0;
                else if(br0==3)boja=1;
                else if(ukL==3)
                {
                    if(br0==1)boja=1;
                    else boja=0;
                }

                //cout<<i<<","<<d<<","<<br0<<"   "<<br1<<","<<ukL<<","<<boja<<endl;

                for(int j=0;j<=d;j+=4)
                {
                    int p=(i+j)%n;
                    int pre=(p-1+n)%n;
                    int posle=(p+1)%n;

                    if(boja!=2 && boja!=b[p])continue;

                    long long preee=1LL;
                    if(j!=0)
                    {
                        preee=dp[i][pre][br0];
                    }

                    long long posleee=1LL;
                    if(j!=d)
                    {
                        int nule=br0 + (b[p]==0);
                        if(ukL==3)nule=0;

                        posleee=dp[posle][kraj][nule];
                    }

                    dp[i][kraj][br0]+=(preee*posleee)%MOD;
                    dp[i][kraj][br0]%=MOD;

                    //cout<<d<<" "<<i<<" "<<br0<<" "<<j<<" "<<endl;
                    //for(int br0=0;br0<4;br0++)cout<<dp[0][0][br0]<<",";
                    //c/out<<endl<<endl;
                }
            }

            /*cout<<d<<" "<<i<<endl;
            for(int dd=0;dd<n;dd++)
            {
                cout<<d<<":  ";
                for(int ii=0;ii<n;ii++)
                {
                    int kraj=(ii+dd)%n;
                    for(int br0=0;br0<4;br0++)cout<<dp[ii][kraj][br0]<<",";
                    cout<<" ";
                }
                cout<<endl;
            }
            cout<<endl<<endl;*/
        }

    /*for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int br0=0;br0<4;br0++)
            {
                cout<<dp[i][j][br0]<<",";
            }
            cout<<" ";
        }
        cout<<endl;
    }

    for(int d=0;d<n;d++)
    {
        cout<<d<<":  ";
        for(int i=0;i<n;i++)
        {
            int kraj=(i+d)%n;
            for(int br0=0;br0<4;br0++)cout<<dp[i][kraj][br0]<<",";
            cout<<" ";
        }
        cout<<endl;
    }*/


    return dp[0][n-1][0];
}