```cpp
#include<bits/stdc++.h>
const int N = 50;
typedef long long LL;
using namespace std;
int a[N], p[N], t[N], s, z[N];
int k[N], n, m[N], M, ans[N], cnt;
int read() {int res = 0;bool bo = false;char ch = getchar();while(ch < '0' || ch > '9') bo = (ch == '-'), ch = getchar();while(ch >= '0' && ch <= '9') res = (res << 1) + (res << 3) + (ch ^ 48), ch = getchar();return bo ? -res : res;}
int ksm(int a, int b, int p){
int res = 1;
for(; b; b >>= 1, a = (LL)a * a % p) if(b & 1) res = (LL)res * a % p;
return res;
}
int exLucas(int n, int p, int o){
return n < p ? t[n] : (LL)t[n % o] * exLucas(n / p, p, o) * ksm(s, n / o, p) % p % p;
}
void exgcd(int a, int b, int &x, int &y){
if(b == 0){x = 1; y = 0; return;}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
int inv(int a, int b){
int x, y;
exgcd(a, b, x, y);
return ((x % b) + b) % b;
}
int fuck(int n, int p){
int ans = 0;
for(; n; n /= p) ans += n / p;
return ans;
}
int work(int n, int m){
int ans = 0;
for(int i = 1; i <= cnt; ++i){
s = 1; t[1] = 1;
for(int j = 2; j <= z[i]; ++j)
if(j % p[i]) s = (s * j) % p[i], t[i] = t[i - 1] * j % p[i];
else t[i] = t[i - 1];
// printf("s:%d | %d\n", s, z[i]);
int n1 = exLucas(n, p[i], z[i]), m1 = exLucas(m, p[i], z[i]), m2 = exLucas(n - m, p[i], z[i]);
// cout<<n1<<" | "<<m1<<" | "<<m2<<endl;
m1 = inv(m1, z[i]); m2 = inv(m2, z[i]);
int n2 = fuck(n, p[i]) - fuck(m, p[i]) - fuck(n - m, p[i]);
int b = m1 * m2 % z[i] * ksm(p[i], n2, z[i]) % z[i] * n1 % z[i];
// cout<<b<<endl;
exgcd(M / z[i], z[i], m1, m2);
m1 = (m1 % M + M) % M;
ans = (ans + m1 * z[i] % M * b % M) % M;
}
return ans;
}
int main()
{
M = read(); n = read(); int x = read(), sum = 0, l = M;
for(int i = 1; i <= x; ++i){
k[i] = read();
sum += k[i];
}
if(sum > n){puts("Impossible");return 0;}
// k[++x] = n - sum;
for(int i = 2; i * i <= l; ++i){
if(l % i == 0){
p[++cnt] = i; z[cnt] = 1;
while(l % i == 0) l /= i, a[cnt]++, z[cnt] *= i;
}
}
if(l > 1) p[++cnt] = l, a[cnt] = 1, z[cnt] = l;
int ans = 1;
for(int i = 1; i <= x; ++i){
ans *= work(n, k[i]);
n -= k[i];
}
printf("%d", ans);
return 0;
}
```
by __wfx @ 2019-06-08 07:56:49
哪位蒟佬救救我
by __wfx @ 2019-06-08 07:57:25
@[zh_dou](/space/show?uid=123081)
by __wfx @ 2019-06-08 07:58:21
@[__wfx](/space/show?uid=162451) 蒟蒻表示看不懂巨佬的代码
by zh_dou @ 2019-06-08 09:16:06
@[zh_dou](/space/show?uid=123081) *****压行
by zh_dou @ 2019-06-08 09:16:26
@[__wfx](/space/show?uid=162451)
Orz
by 我惠美如画 @ 2019-06-08 09:16:53
@[__wfx](/space/show?uid=162451) tql%%%
by enceladus @ 2019-06-08 09:18:08
我是郑好
by enceladus @ 2019-06-08 09:18:38