#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;

const ll MOD=1000000007;

ll N;
ll A[100000];

int main(){
    cin>>N;
    rep(i,N) cin>>A[i];

    ll cnt0=3;
    vector<ll> cnt(N,0);
    ll ans=1;
    rep(i,N){
        if(A[i]==0){
            ans*=cnt0;
            cnt0--;
        }else{
            ans*=cnt[A[i]-1];
            cnt[A[i]-1]--;
        }
        cnt[A[i]]++;
        ans%=MOD;
    }
    ans+=MOD;
    ans%=MOD;

    cout<<ans<<endl;
}
#include <bits/stdc++.h>
using namespace std;
#define N 300100
const int mod=1e9+7;

int a[8][2],f[N][8];
char s[N];

int main()
{
	a[0][0]=1; a[0][1]=2;
	a[1][0]=3; a[1][1]=4;
	a[2][0]=5; a[2][1]=6;
	a[3][0]=a[3][1]=1;
	a[4][0]=1; a[4][1]=2;
	a[5][0]=7; a[5][1]=2;
	a[6][0]=a[6][1]=6;
	a[7][0]=a[7][1]=5;
	scanf("%s",s);
	int n=strlen(s);
	f[n][2]=f[n][6]=1;
	for (int i=n-1;i>=0;i--)
		for (int j=0;j<8;j++)
		{
			if (s[i]!='1') (f[i][j]+=f[i+1][a[j][0]])%=mod;
			if (s[i]!='0') (f[i][j]+=f[i+1][a[j][1]])%=mod;
		}
	printf("%d\n",f[0][0]);
	return 0;
}