代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#define mod 1000000007
using namespace std;
int s[6];
long long f[17][17][17][17][17][7];
long long dfs(int a1,int a2,int a3,int a4,int a5,int x)
{
    if(a1==0 && a2==0 && a3==0 && a4==0 && a5==0)f[a1][a2][a3][a4][a5][x]=1;
    if(f[a1][a2][a3][a4][a5][x]!=0)return f[a1][a2][a3][a4][a5][x];
    long long ans=0;
    if(a1!=0)
    {
        int he=a1;if(x==2)he--;
        ans+=he*dfs(a1-1,a2,a3,a4,a5,1);ans%=mod;
    }
    if(a2!=0)
    {
        int he=a2;if(x==3)he--;
        ans+=he*dfs(a1+1,a2-1,a3,a4,a5,2);ans%=mod;
    }
    if(a3!=0)
    {
        int he=a3;if(x==4)he--;
        ans+=he*dfs(a1,a2+1,a3-1,a4,a5,3);ans%=mod;
    }
    if(a4!=0)
    {
        int he=a4;if(x==5)he--;
        ans+=he*dfs(a1,a2,a3+1,a4-1,a5,4);ans%=mod;
    }
    if(a5!=0)
    {
        int he=a5;
        ans+=he*dfs(a1,a2,a3,a4+1,a5-1,5);ans%=mod;
    }
    f[a1][a2][a3][a4][a5][x]=ans;
    return ans;
}
int main()
{
    int k;
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        int x;
        scanf("%d",&x);
        s[x]++;
    }
    printf("%lld\n",dfs(s[1],s[2],s[3],s[4],s[5],0));
    return 0;
}