J=lambda:map(int,input().split()) N,P,C=J() A=[[]] for a in J(): if a:A[-1]+=[a] else:A+=[[]] a=(C<1)*N*(N+1)//2 for v in A: c={} if C: t=1 for w in v: u,t=t*C%P,w*t%P c[u]=c[u]+1if u in c else 1 a+=c[t]if t in c else 0 else: t=len(v) a-=t*(t+1)//2 print(a)