import itertools N,P,Q=map(int,input().split()) A=list(map(int,input().split())) print(sum(1 for a,b,c,d in itertools.combinations(A,4) if (10**a+9**b+7**c+5**d)%P==Q))