#include using namespace std; #define int long long signed main() { int n,mod,f; cin >> n >> mod >> f; vector a(n); vector p(2e6+1,1), q(2e6+1,1), r(2e6+1,1), s(2e6+1,1); for(int i = 0; i < 2e6; i++) { p[i+1] = p[i]*10%mod; q[i+1] = q[i]*9%mod; r[i+1] = r[i]*7%mod; s[i+1] = s[i]*5%mod; } for(int i = 0; i < n; i++) cin >> a[i]; vector> g(n+1,vector(mod,0)); for(int i = n-1; i >= 0; i--) { g[i][s[a[i]]]++; for(int j = 0; j < mod; j++) g[i][j]+=g[i+1][j]; } int ans = 0; for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) for(int k = j+1; k < n; k++) { int x = (f-p[a[i]]-q[a[j]]-r[a[k]]+mod*mod)%mod; ans+=g[k+1][x]; } cout << ans << endl; }