有n个木块排成一行,从左到右依次编号为1~n。你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块。
所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n。相邻两个木块涂相同色显得很难看,所以你希望统计任意两
个相邻木块颜色不同的着色方案。
Input
第一行为一个正整数k,第二行包含k个整数c1, c2, ... , ck。
Output
输出一个整数,即方案总数模1,000,000,007的结果。
Sample Input
3 1 2 3
Sample Output
10
Hint
100%的数据满足:1 <= k <= 15, 1 <= ci <= 5
感觉和大部分题解差不多;
考虑 dp[ a ][ b ][ c ][ d ][ e ][ last ]表示可以涂1个格子的颜色有 a 种,... ,last 表示上一次使用的颜色编号;
注意long long ;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200004
#define inf 0x3f3f3f3f
#define INF 999999999999
#define rdint(x) scanf("%d",&x)
#define rddoub(x) scanf("%lf",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 1e9 + 7;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-7
typedef pair<int, int> pii;
#define pi acos(-1.0)
#define REP(i,n) for(int i=0;i<(n);i++)
inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }
int k;
int n;
ll num[7];
ll DP[17][17][17][17][17][9];
ll dp(ll a, ll b, ll c, ll d, ll e, ll last) {
if (DP[a][b][c][d][e][last])return DP[a][b][c][d][e][last];
int ans = 0;
if (a)ans = (ans + (a - (last == 2))*dp(a - 1, b, c, d, e, 1)) % (ll)mod;
if (b)ans = (ans + (b - (last == 3))*dp(a + 1, b - 1, c, d, e, 2)) %(ll)mod;
if (c)ans = (ans + (c - (last == 4))*dp(a, b + 1, c - 1, d, e, 3)) % (ll)mod;
if (d)ans = (ans + (d - (last == 5))*dp(a, b, c + 1, d - 1, e, 4)) % (ll)mod;
if (e)ans = (ans + (e - (last == 6))*dp(a, b, c, d + 1, e - 1, 5)) % (ll)mod;
DP[a][b][c][d][e][last] = (ll)ans % mod;
return DP[a][b][c][d][e][last];
}
int main()
{
//ios::sync_with_stdio(false);
rdint(k);
for (int i = 1; i <= k; i++) {
int x;
rdint(x); num[x]++;
}
DP[1][0][0][0][0][1] = 1;
cout << dp(num[1], num[2], num[3], num[4], num[5], 0) << endl;
return 0;
}