#include using namespace std; #include using namespace atcoder; using ll = long long; #define rep(i, n) for(int i = 0; i < n; i++) int main() { int N, p, q; cin >> N >> p >> q; vector a(N); rep(i, N) cin >> a[i]; sort(a.begin(), a.end()); vector v(N); rep(i, N) v[i] = pow_mod(5, a[i], p); vector> vmp(N); for(int i = N - 1; i >= 0; i--) { map mp; if(i < N - 1) mp = vmp[i + 1]; mp[v[i]]++; vmp[i] = mp; } int ans = 0; rep(i, N) { for(int j = i + 1; j < N; j++) { for(int k = j + 1; k < N - 1; k++) { ll s = pow_mod(10, a[i], p) + pow_mod(9, a[j], p) + pow_mod(7, a[k], p); int m = s % p; if(m > q) continue; int r = q - m; ans += vmp[k + 1][r]; } } } cout << ans << endl; }