F[i][0] = \frac{n!}{(n-i)!}(x + 1)^{n-i}
```cpp
#include <bits/stdc++.h>
const int MAXN = 1e3 + 5;
int n, x, p, m;
int F[MAXN][MAXN];
int a[MAXN];
int FastPow(int a, int b) {
int res = 1;
for(; b; b >>= 1, a = 1LL * a * a % p)
if(b & 1) res = 1LL * res * a % p;
return res;
}
int main() {
int res = 0;
scanf("%d%d%d%d", &n, &x, &p, &m);
for(int i = 0; i <= m; i++) scanf("%d", a + i);
for(int i = 0; i <= m; i++) {
int s = 1;
for(int j = n - i + 1; j <= n; j++) s = 1LL * s * j % p;
F[i][0] = 1LL * s * FastPow(x + 1, n - i) % p;
}
for(int i = 1; i <= m; i++) {
for(int j = 0; j <= m - i; j++) {
F[j][i] = (1LL * x * F[j + 1][i - 1] % p + 1LL * j * F[j][i - 1] % p) % p;
}
}
for(int i = 0; i <= m; i++) res = (res + 1LL * a[i] * F[0][i]) % p;
printf("%d\n", res);
return 0;
}
```