Start: Feb, 20, 2019 12:00:00
2018-2019 ACM集训队冬季集训第一次考核
End: Feb, 20, 2019 17:00:00
Time elapsed:
Time remaining:

Problem_ID: A
Result: Accepted
Time: 5ms
Memory: 1092kB
Author: 2017212212172
In contest: 1260

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
const LL mod = 1e9 + 7;
int ax[20];
char ch[100];
int n;
long long sum = 0;
void dfs(int pos, int x,long long num) {
	if (pos == n) {
		sum = (sum + num) % mod;
	}
	if (ch[pos - 1] == '1') {
		if (x == 1) {
			if (ax[3] >= 1) {
				ax[3]--;
				dfs(pos + 1, 3, (ax[3]+1)%mod*num%mod);
				ax[3]++;
			}
		}
		else if (x == 3) {
			if (ax[1] >= 1) {
				ax[1]--;
				dfs(pos + 1, 1, (ax[1] + 1) % mod*num%mod);
				ax[1]++;
			}
		}
	}
	else if (ch[pos - 1] == '0') {
		if (x == 1) {
			if (ax[1] >= 1) {
				ax[1]--;
				dfs(pos + 1, 1, (ax[1] + 1) % mod*num%mod);
				ax[1]++;
			}
			if (ax[2] >= 1) {
				ax[2]--;
				dfs(pos + 1, 2, (ax[2] + 1) % mod*num%mod);
				ax[2]++;
			}
		}
		else if (x == 2) {
			if (ax[1] >= 1) {
				ax[1]--;
				dfs(pos + 1, 1, (ax[1] + 1) % mod*num%mod);
				ax[1]++;
			}
			if (ax[2] >= 1) {
				ax[2]--;
				dfs(pos + 1, 2, (ax[2] + 1) % mod*num%mod);
				ax[2]++;
			}
			if (ax[3] >= 1) {
				ax[3]--;
				dfs(pos + 1, 3, (ax[3] + 1) % mod*num%mod);
				ax[3]++;
			}
		}
		else if (x == 3) {
			if (ax[3] >= 1) {
				ax[3]--;
				dfs(pos + 1, 3, (ax[3] + 1) % mod*num%mod);
				ax[3]++;
			}
			if (ax[2] >= 1) {
				ax[2]--;
				dfs(pos + 1, 2, (ax[2] + 1) % mod*num%mod);
				ax[2]++;
			}
		}
	}
}
int main(void) {
	scanf("%d", &n);
	sum = 0;
	memset(ax, 0, sizeof(ax));
	for (int i = 0; i < n; i++) {
		int a;
		scanf("%d", &a);
		ax[a]++;
	}
	scanf("%s", ch);
	if (ch[0] == '1') {
		if (ax[1] >= 1) {
			ax[1]--;
			dfs(1, 1, ax[1]+1);
			ax[1]++;
		}
		if (ax[3] >= 1) {
			ax[3]--;
			dfs(1, 3, ax[3]+1);
			ax[3]++;
		}
	}
	else {
		if (ax[1] >= 1) {
			ax[1]--;
			dfs(1, 1, ax[1]+1);
			ax[1]++;
		}
		//printf("%lld\n", sum);
		if (ax[2] >= 1) {
			ax[2]--;
			dfs(1, 2, ax[2]+1);
			ax[2]++;
		}
		//printf("%lld\n", sum);
		if (ax[3] >= 1) {
			ax[3]--;
			dfs( 1, 3, ax[3]+1);
			ax[3]++;
		}
		//printf("%lld\n", sum);
	}
	printf("%lld\n", sum);
	return 0;
}