考虑不同次数有多少个
不要考虑不同颜色有多少个
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int cnt[6];// 表示 可以使用i次的 颜色有多少个
ll vis[16][16][16][16][16][16];
int k;
const int mod = 1e9+7;
ll dfs(int t1, int t2, int t3, int t4, int t5, int last)
{
ll ans = 0;
if(t1+t2+t3+t4+t5 == 0)return 1;
if(vis[t1][t2][t3][t4][t5][last]!= 0)return vis[t1][t2][t3][t4][t5][last];
if(t1)//如果使用的1次的还存在
{
ans+=(t1- (last == 2))*dfs(t1-1, t2, t3, t4, t5, 1);/******last == 2 的 作用: 如果上次是使用2次的,这次就变成了使用1次的了, 因为不能连续图相同的颜色,所以那个1次要减去******************/
ans%=mod;
}
if(t2)
{
ans+=(t2 - (last == 3))*dfs(t1+1, t2-1, t3 ,t4, t5, 2);// 为甚么 要+1 -1, 这次使用颜色是2 次的 , 所以 那个颜色在下一个状态 只能使用一次了, 所以要+1, -1.
ans%=mod;
}
if(t3)
{
ans+=(t3- (last == 4))*dfs(t1, t2+1, t3-1, t4, t5, 3);
ans%=mod;
}
if(t4)
{
ans+=(t4- (last == 5))*dfs(t1, t2, t3+1, t4-1, t5, 4);
ans%=mod;
}
if(t5)
{
ans+=t5*dfs(t1, t2, t3, t4+1, t5-1, 5);
ans%=mod;
}
return vis[t1][t2][t3][t4][t5][last] =ans;
}
int main()
{
memset(cnt, 0, sizeof(cnt));
memset(vis, 0, sizeof(vis));
scanf("%d", &k);
for(int i = 0;i < k;i++)
{
int c;
scanf("%d", &c);
cnt[c]++;
}
ll ans = dfs(cnt[1], cnt[2], cnt[3], cnt[4], cnt[5], 0);
printf("%lld", ans);
return 0;
}