https://www.cnblogs.com/Gloid/p/10629779.html 这一场的D。
#include<bits/stdc++.h>
using namespace std;
#define N 1010
#define M 5010
#define P 998244353
int n,m,a[N],f[N][M],fac,inv[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
#endif
cin>>n>>m;
for (int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);reverse(a+1,a+n+1);
inv[0]=inv[1]=1;for (int i=2;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
fac=1;for (int i=2;i<=n;i++) fac=1ll*fac*i%P;
f[0][m]=1;
for (int i=0;i<n;i++)
for (int j=0;j<=m;j++)
f[i+1][j]=(f[i+1][j]+1ll*f[i][j]*(n-i-1)%P*inv[n-i])%P,
f[i+1][j%a[i+1]]=(f[i+1][j%a[i+1]]+1ll*f[i][j]*inv[n-i])%P;
for (int i=m;i>=0;i--)
if (f[n][i]) {printf("%d\n%d",i,1ll*fac*f[n][i]%P);break;}
}