简单容斥一下变成统计至少有i对连续的1234
然后随便组合数算一下就完了
Code:
#include<bits/stdc++.h>
#define mod 998244353
using namespace std;
inline int read(){
int res=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-f;ch=getchar();}
while(isdigit(ch)) {res=(res<<1)+(res<<3)+(ch^48);ch=getchar();}
return res*f;
}
const int N=2e3+5;
inline int add(int x,int y){x+=y;if(x>=mod) x-=mod;if(x<0) x+=mod;return x;}
inline int mul(int x,int y){return 1ll*x*y%mod;}
inline void inc(int &x,int y){x+=y;if(x>=mod) x-=mod;if(x<0) x+=mod;}
inline int ksm(int a,int b){int res=1;for(;b;b>>=1,a=mul(a,a)) if(b&1) res=mul(res,a);return res;}
int n,a,b,c,d,m;
int fac[N],ifac[N],tmp[N];
inline void init(int n){
fac[0]=fac[1]=ifac[0]=ifac[1]=1;
for(int i=2;i<=n;i++) fac[i]=mul(fac[i-1],i);
for(int i=2;i<=n;i++) ifac[i]=mul((mod-mod/i),ifac[mod%i]);
for(int i=2;i<=n;i++) ifac[i]=mul(ifac[i],ifac[i-1]);
}
int ans=0;
int main(){
n=read();a=read();b=read();c=read();d=read();
init(n);
m=min(n>>2,min(min(a,b),min(c,d)));
for(int i=0;i<=m;i++){
int pw=(i&1)?-1:1;
memset(tmp,0,sizeof(tmp));int res=0;
for(int j=0;j<=a-i;j++)
for(int k=0;k<=min(n-4*i-j,b-i);k++)
inc(tmp[j+k],mul(ifac[j],ifac[k]));
for(int j=0;j<=c-i;j++)
for(int k=0;k<=min(n-4*i-j,d-i);k++)
inc(res,mul(mul(ifac[j],ifac[k]),tmp[n-4*i-j-k]));
res=mul(res,fac[n-3*i]);
res=mul(res,ifac[i]);
inc(ans,mul(pw,res));
}
cout<<ans;
return 0;
}