题意:
思路:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 16;
const ll mod = 1e9 + 7;
int f[N][N][N][N][N][6];
int num[6];
ll dfs(int c1,int c2,int c3,int c4,int c5,int last){
if(f[c1][c2][c3][c4][c5][last] != -1) return f[c1][c2][c3][c4][c5][last];
ll res = 0;
if (c1) res = (res + (c1 - (last == 2)) * dfs(c1 - 1, c2, c3, c4, c5, 1)) % mod;
if (c2) res = (res + (c2 - (last == 3)) * dfs(c1 + 1, c2 - 1, c3, c4, c5, 2)) % mod;
if (c3) res = (res + (c3 - (last == 4)) * dfs(c1, c2 + 1, c3 - 1, c4, c5, 3)) % mod;
if (c4) res = (res + (c4 - (last == 5)) * dfs(c1, c2, c3 + 1, c4 - 1, c5, 4)) % mod;
if (c5) res = (res + c5 * dfs(c1, c2, c3, c4 + 1, c5 - 1, 5)) % mod;
return f[c1][c2][c3][c4][c5][last]=res;
}
int main(){
int n;scanf("%d",&n);
memset(f,-1,sizeof(f));
for(int i = 1;i <= 5;i++) f[0][0][0][0][0][i] = 1;
for(int i = 1,x;i <= n;i++){
scanf("%d",&x);
num[x]++;
}
printf("%lld\n",dfs(num[1],num[2],num[3],num[4],num[5],0));
return 0;
}