from collections import defaultdict import copy N, P, Q=map(int, input().split()) A=list(map(int, input().split())) stset=[None for i in range(N)] for i in range(len(A)-1, -1, -1): if i==len(A)-1: obj=defaultdict(int) obj[pow(5, A[i], P)]+=1 stset[i]=obj continue obj=copy.deepcopy(stset[i+1]) obj[pow(5, A[i], P)]+=1 stset[i]=obj ans=0 for a in range(N-4+1): for b in range(a+1, N-3+1): for c in range(b+1, N-2+1): v=pow(10, A[a], P)+pow(9, A[b], P)+pow(7, A[c], P) v%=P target=(Q-v)%P ans+=stset[c+1][target] print(ans)