import itertools N,P,Q=map(int,input().split()) A=list(map(int,input().split())) # 200C4=64684950通り count=0 for a in itertools.combinations(A,4): a=sorted(a) if (10**a[0]+9**a[1]+7**a[2]+5**a[3])%P==Q: count+=1 print(count)