1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> typedef long long ll; using namespace std; const ll maxn=16,mod=1e9+7; ll f[maxn][maxn][maxn][maxn][maxn][6],b[6];
ll dfs(ll a,ll b,ll c,ll d,ll e,ll last){ if(f[a][b][c][d][e][last]!=-1)return f[a][b][c][d][e][last]; if(a+b+c+d+e==0)return 1; ll ans=0; if(a)ans+=(a-(last==2))*dfs(a-1,b,c,d,e,1); if(b)ans+=(b-(last==3))*dfs(a+1,b-1,c,d,e,2); if(c)ans+=(c-(last==4))*dfs(a,b+1,c-1,d,e,3); if(d)ans+=(d-(last==5))*dfs(a,b,c+1,d-1,e,4); if(e)ans+=e*dfs(a,b,c,d+1,e-1,5); ans%=mod; f[a][b][c][d][e][last]=ans; return ans; }
int main() { memset(f,-1,sizeof f); int k; scanf("%d",&k); for(int a,i=1;i<=k;i++)scanf("%d",&a),b[a]++; printf("%lld",dfs(b[1],b[2],b[3],b[4],b[5],0)); return 0; }
|