题意:



思路:
%E8%A1%A8%E7%A4%BA%E5%BD%93%E5%89%8D%E8%BF%98%E8%83%BD%E6%B6%821%E4%B8%AA%E6%A0%BC%E5%AD%90%E7%9A%84%E6%B2%B9%E6%BC%86%E6%9C%89c_1%E7%A7%8D%EF%BC%8C2%E4%B8%AA%E6%A0%BC%E5%AD%90%E7%9A%84%E6%9C%89c_2%E7%A7%8D&preview=true)



%20*%20f(c_1%20-%201%2C%20c_2%2C%20c_3%2C%20c_4%2C%20c_5%2C%201)&preview=true)
)&preview=true)




%20*%20f(c_1%20%2B%201%2C%20c_2%20-%201%2C%20c_3%2C%20c_4%2C%20c_5%2C%202)&preview=true)
%20*%20f(c_1%2C%20c_2%20%2B%201%2C%20c_3%20-%201%2C%20c_4%2C%20c_5%2C%203)&preview=true)
%20*%20f(c_1%2C%20c_2%2C%20c_3%20%2B%201%2C%20c_4%20-%201%2C%20c_5%2C%204)&preview=true)


&preview=true)

%20%3D%201%2C%E7%94%A8%E4%B8%8A%E8%AE%B0%E5%BF%86%E5%8C%96%EF%BC%8C%E9%81%BF%E5%85%8D%E5%A4%9A%E6%AC%A1%E6%90%9C%E7%B4%A2%E5%88%B0%E7%9B%B8%E5%90%8C%E7%9A%84%E7%8A%B6%E6%80%81&preview=true)
#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;
}