提交时间:2024-07-19 12:32:58
运行 ID: 30375
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" int n,q; int ct[61][61][2]; const int M=1e9+7; int fac[300300],iv[300030]; inline int qp(int a,int x){ int res=1; while(x){ if(x&1)res=res*a%M; a=a*a%M; x>>=1; } return res; } inline int inv(int x){ return qp(x,M-2); } inline int A(int n,int m){ return fac[n]*iv[n-m]%M; } inline void bd(int l,int r,int c_1,int c1){ if(l>r){ ct[c_1][c1][0]++; return ; } ct[c_1][c1][1]++; if(l==r){ ct[c_1+1][c1][0]++; ct[c_1][c1+1][0]++; return ; } int mid=l+r>>1; bd(mid+1,r,c_1+1,c1); bd(l,mid-1,c_1,c1+1); } inline int S(int n){return n*(n+1)/2;} inline int S(int l,int r){return (S(r)-S(l-1))%M;} signed main(){ ios::sync_with_stdio(0); // freopen("binary.in","r",stdin); // freopen("binary.out","w",stdout); fac[0]=iv[0]=1; for(int i=1;i<=3e5;i++) fac[i]=fac[i-1]*i%M,iv[i]=inv(fac[i])%M; cin>>n>>q; bd(1,n,0,0); while(q--){ int x; cin>>x; int C0=1,C_1=x-1,C1=n-x; //a:<0 b:>0 c:=0 int w0=x,w_1=S(1,x-1)*inv(C_1)%M,w1=S(x+1,n)*inv(C1)%M; int res=0; for(int a=0;a<=30&&a<=C_1;a++) for(int b=0;b<=30&&b<=C1;b++) for(int c=0;c<=1;c++){ int f=fac[n-a-b-c]*A(C_1,a)%M*A(C1,b)%M*A(C0,c)%M*ct[a][b][c]%M; res=(res+f*(a*w_1%M+b*w1%M+c*w0%M)%M)%M; } printf("%lld\n",res); } cout.flush(); return 0; }