from collections import defaultdict from itertools import product N, M, K = map(int, input().split()) op, *B = input().split() A = [int(input()) for _ in range(N)] B = list(map(int, B)) if K == 1: print(N*M) exit() def solve_add(): D = defaultdict(int) for b in B: D[b % K] += 1 ans = 0 for a in A: ans += D[(-a) % K] print(ans) def factorization(n): if n == 1: return [[1, 1]] temp = n ans = [] for i in range(2, int(n**0.5+1.01)): if temp % i == 0: ct = 0 while temp % i == 0: temp //= i ct += 1 ans.append([i, ct]) if temp != 1: ans.append([temp, 1]) return ans def solve_prod(): fact = factorization(K) plist = [p[0] for p in fact] pcount = [p[1] for p in fact] D = defaultdict(int) n = len(fact) for b in B: t = [0]*n for i in range(n): while t[i] < pcount[i] and b % plist[i] == 0: t[i] += 1 b //= plist[i] D[tuple(t)] += 1 for i in range(n): nD = defaultdict(int) for t in product(*(reversed(range(pcount[i]+1)) for i in range(n))): t = tuple(t) nD[t] += D[t] if t[i] == 0: continue nt = [t[j]-(i == j) for j in range(n)] nD[tuple(nt)] += nD[t] D = nD ans = 0 for a in A: t = [0]*n for i in range(n): while t[i] < pcount[i] and a % plist[i] == 0: t[i] += 1 a //= plist[i] t = [max(0, pcount[i]-t[i]) for i in range(n)] ans += D[tuple(t)] print(ans) if op == '+': solve_add() else: solve_prod()