You are given a prime number $p$, $n$ integers $a_1, a_2, \ldots, a_n$, and an integer $k$. 

Find the number of pairs of indexes $(i, j)$ ($1 \le i < j \le n$) for which $(a_i + a_j)(a_i^2 + a_j^2) \equiv k \bmod p$.


-----Input-----

The first line contains integers $n, p, k$ ($2 \le n \le 3 \cdot 10^5$, $2 \le p \le 10^9$, $0 \le k \le p-1$). $p$ is guaranteed to be prime.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le p-1$). It is guaranteed that all elements are different.


-----Output-----

Output a single integer — answer to the problem.


-----Examples-----
Input
3 3 0
0 1 2

Output
1
Input
6 7 2
1 2 3 4 5 6

Output
3


-----Note-----

In the first example:

$(0+1)(0^2 + 1^2) = 1 \equiv 1 \bmod 3$.

$(0+2)(0^2 + 2^2) = 8 \equiv 2 \bmod 3$.

$(1+2)(1^2 + 2^2) = 15 \equiv 0 \bmod 3$.

So only $1$ pair satisfies the condition.

In the second example, there are $3$ such pairs: $(1, 5)$, $(2, 3)$, $(4, 6)$.

Difficulty: HARD

Solutions:
import sys
import math

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def read_float_line():
	return [float(v) for v in sys.stdin.readline().split()]

def nc2(n):
	return n * (n - 1) // 2
(n, p, k) = read_int_line()
a = read_int_line()
ans = []
for i in range(n):
	ans.append((a[i] ** 4 - a[i] * k) % p)
d = {}
for i in ans:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
al = list(d.values())
ans = 0
for i in al:
	if i != 1:
		ans += nc2(i)
print(ans)


(n, p, k) = map(int, input().split())
arr = list(map(int, input().split()))
cnt = dict()

def f(a):
	return ((pow(a, 4, p) - k * a) % p + p) % p

def g(a):
	return a * (a - 1) // 2
res = 0
for a in arr:
	v = f(a)
	if v in cnt:
		cnt[v] += 1
	else:
		cnt[v] = 1
for a in arr:
	v = f(a)
	res += g(cnt[v])
	cnt[v] = 0
print(res)


from collections import Counter
(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
freqs = Counter([(x ** 4 - k * x) % p for x in a])
ans = sum([(x - 1) * x // 2 for x in freqs.values()])
print(ans)


cond = [int(x) for x in input().split()]
n = cond[0]
p = cond[1]
k = cond[2]
a = [int(x) for x in input().split()]
b = [(x ** 4 - k * x) % p for x in a]
d = {}
r = 0
for x in b:
	if x not in d:
		d[x] = 0
	r += d[x]
	d[x] += 1
print(r)


(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
a = [(i ** 4 - i * k) % p for i in a]
c = {i: 0 for i in a}
for i in a:
	c[i] += 1
s = 0
for i in c:
	i = c[i]
	s += i * (i - 1) // 2
print(s)


(n, p, k) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
di = {}
ans = 0
for i in l:
	x = (i * i * i * i - k * i) % p
	if x in di:
		ans += di[x]
		di[x] += 1
	else:
		di[x] = 1
print(ans)


def Sum(n):
	return n * (n - 1) // 2

def Solver(p, k, a):
	aux = []
	for i in a:
		aux.append((i ** 4 - i * k) % p)
	dic = {}
	for i in aux:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	val = list(dic.values())
	sol = 0
	for i in val:
		if i > 1:
			sol += Sum(i)
	return sol

def Main():
	(n, p, k) = map(int, input().split())
	a = list(map(int, input().split()))
	sol = Solver(p, k, a)
	print(sol)
Main()


(n, p, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
d = dict()
for i in range(n):
	now = a[i] * a[i] * a[i] * a[i] - k * a[i]
	now %= p
	if now in d:
		d[now] += 1
	else:
		d[now] = 1
ans = 0
for i in range(n):
	now = a[i] * a[i] * a[i] * a[i] - k * a[i]
	now %= p
	ans += d[now] - 1
ans //= 2
print(ans)


(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
b = dict()
ans = 0
for i in a:
	j = (i ** 4 - k * i) % p
	c = b.get(j, 0)
	ans += c
	b[j] = c + 1
print(ans)


def expmod(a, n, m):
	if n == 0:
		return 1
	elif n == 1:
		return a % m
	res = n / 2
	rem = n % 2
	temp = expmod(a, res, m)
	if rem == 1:
		return temp * temp * a % m
	else:
		return temp * temp % m
(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
holes = {}
res = 0
for i in range(0, n):
	temp = ((expmod(a[i], 4, p) - k * a[i]) % p + p) % p
	if temp in holes:
		res += holes[temp]
		holes[temp] += 1
	else:
		holes[temp] = 1
print(int(res))


(n, p, k) = map(int, input().split())
arr = list(map(int, input().split()))
dct = {}
answer = 0
for elem in arr:
	val = (elem ** 4 - k * elem) % p
	if val in dct:
		answer += dct[val]
		dct[val] += 1
	else:
		dct[val] = 1
print(answer)


(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
cnt = {}
b = []
ans = 0
for i in a:
	b.append((i ** 4 - k * i) % p)
for i in b:
	if i in cnt:
		cnt[i] += 1
		ans += cnt[i] - 1
	else:
		cnt[i] = 1
print(ans)


(n, p, k) = map(int, input().split())
a = [int(x) for x in input().split()]
m = {}
for i in a:
	t = (i * i * i * i - k * i) % p
	if t in m:
		m[t] += 1
	else:
		m[t] = 1
ans = 0
for key in m:
	if m[key] >= 2:
		ans += (m[key] - 1) * m[key] // 2
print(ans)


line = input()
line = line.split()
line = list(map(lambda x: int(x), line))
n = line[0]
p = line[1]
k = line[2]
inputs = input()
inputs = inputs.split()
inputs = list(map(lambda x: int(x), inputs))
dict = {}
counter = 0
for i in range(n):
	exped = ((inputs[i] ** 2) ** 2 - k * inputs[i]) % p
	if dict.get(exped, None) is None:
		dict[exped] = 0
	counter = counter + dict[exped]
	dict[exped] = dict[exped] + 1
print(counter)


(n, p, k) = map(int, input().split())
d = {}
ans = 0
b = list(map(int, input().split()))
for i in range(n):
	a = b[i]
	val = (a ** 4 - k * a) % p
	if d.get(val) == None:
		d[val] = 1
	else:
		ans += d[val]
		d[val] += 1
print(ans)


from collections import Counter
(n, p, k) = map(int, input().split())
arr = [(x ** 4 - k * x) % p for x in list(map(int, input().split()))]
print(sum([x * (x - 1) // 2 for x in Counter(arr).values()]))


from collections import defaultdict
(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
d = defaultdict(int)
ans = 0
for el in a:
	val = (el ** 4 - k * el) % p
	ans += d[val]
	d[val] += 1
print(ans)


from collections import defaultdict
(n, p, k) = map(int, input().split())
keys = list(map(int, input().split()))
dictionary = defaultdict(lambda : 0)
answer = 0
for key in keys:
	answer += dictionary[(pow(key, 4, p) - k * key) % p]
	dictionary[(key ** 4 - k * key) % p] += 1
print(answer)


(n, p, k) = map(int, input().split())
l = list(map(int, input().split()))
d = dict()
for y in range(n):
	x = pow(l[y], 4) - l[y] * k
	if x % p not in d:
		d[x % p] = 1
	else:
		d[x % p] += 1
ct = 0
for (k, v) in d.items():
	ct += v * (v - 1) // 2
print(ct)


(n, p, k) = map(int, input().split())
arr = list(map(int, input().split()))
dict1 = {}
for i in range(n):
	val = (pow(arr[i], 4, p) - k * arr[i] % p + p) % p
	try:
		dict1[val] += 1
	except:
		KeyError
		dict1[val] = 1
ans = 0
for i in dict1.keys():
	ans += dict1[i] * (dict1[i] - 1) // 2
print(ans)


from collections import defaultdict
(n, p, k) = list(map(int, input().split()))
l = list(map(int, input().split()))
count = 0
dic = defaultdict(int)
for i in range(n):
	value = (l[i] ** 4 % p - k * l[i] % p) % p
	count += dic[value]
	dic[value] = dic[value] + 1
print(count)


from collections import Counter
(n, p, k) = map(int, input().split())
a = map(int, input().split())
ans = 0
for (k, v) in Counter([(x ** 4 - x * k) % p for x in a]).items():
	ans += v * (v - 1) >> 1
print(ans)


(n, p, k) = map(int, input().split())
arr = list(map(int, input().split()))
d = {}
for i in arr:
	x = i * i % p * i % p * i % p
	y = i * k % p
	cc = (x - y + p) % p
	if cc in d:
		d[cc] += 1
	else:
		d[cc] = 1
ans = 0
for (key, value) in d.items():
	cc = value * (value - 1) // 2
	ans += cc
print(ans)


(n, p, k) = map(int, input().split())
ar = list(map(int, input().split()))
ar = [(i ** 4 - k * i + p ** 4) % p for i in ar]
d = dict()
for i in ar:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
ans = 0
for key in d:
	ans += d[key] * (d[key] - 1) // 2
print(ans)


(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
h = []
for i in a:
	h.append((i * i * i * i - k * i) % p)
from collections import Counter
j = Counter(h)
su = 0
for i in j:
	su += j[i] * j[i] - j[i]
print(su // 2)


ln = [int(i) for i in input().split(' ')]
n = ln[0]
p = ln[1]
k = ln[2]
a = [int(i) for i in input().split(' ')]
cts = {}
ctsc = {}
ans = []
nm = 0
for i in range(0, len(a)):
	an = (a[i] ** 4 - k * a[i]) % p
	if an in ctsc:
		nm += ctsc[an]
		ctsc[an] += 1
	else:
		ctsc[an] = 1
print(nm)


(n, p, k) = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]
b = []
for i in range(n):
	b.append((pow(a[i], 4, p) - k * a[i]) % p)
mydic = {}
for i in b:
	mydic[i] = 0
for i in b:
	mydic[i] += 1
ans = 0
for t in mydic:
	ans += mydic[t] * (mydic[t] - 1) // 2
print(ans)


(n, p, k) = list(map(int, input().strip().split()))
a = list(map(int, input().strip().split()))
d = dict()
for el in a:
	r = 1
	r *= el
	r %= p
	r *= el
	r %= p
	r *= el
	r %= p
	r *= el
	r %= p
	s = k * el % p
	r -= s
	r %= p
	try:
		d[r] += 1
	except:
		d[r] = 1
c = 0
for k in d.keys():
	c += d[k] * (d[k] - 1) // 2
print(c)


import collections
(n, p, k) = map(int, input().split())
arr = list(map(int, input().split()))
dic = collections.defaultdict(int)
for i in range(n):
	tmp = arr[i] ** 4 - arr[i] * k
	tmp %= p
	dic[tmp] += 1
ans = 0
for key in dic.keys():
	ans += dic[key] * (dic[key] - 1) // 2
print(ans)


from collections import defaultdict as dc
(n, p, k) = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n):
	x = (a[i] ** 2 % p) ** 2 % p - k * a[i] % p
	if x < 0:
		b.append(x + p)
	else:
		b.append(x)
d = dc(int)
for i in b:
	d[i] += 1

def c(n):
	return n * (n - 1) // 2
cnt = 0
for i in list(d.values()):
	cnt += c(i)
print(cnt)


def main():
	(n, p, k) = map(int, input().split())
	ans = list(map(int, input().split()))
	for i in range(n):
		temp = ans[i]
		ans[i] = (temp ** 4 - temp * k) % p
	mp = {}
	for i in ans:
		mp.setdefault(i, 0)
		mp[i] = mp[i] + 1
	temp = 0
	for i in mp.values():
		temp = i * (i - 1) // 2 + temp
	print(temp)
main()


l1 = input().split()
n = int(l1[0])
p = int(l1[1])
k = int(l1[2])
l = input().split()
A = {}
count = 0
e = []
for i in range(n):
	b = int(l[i])
	t = b * b % p * (b * b % p) - k * b % p
	t = t % p
	if t in A:
		A[t] += 1
	else:
		A[t] = 1
for x in A:
	count += int(A[x] * (A[x] - 1) / 2)
print(count)


(n, p, k) = map(int, input().split())
d = dict()
a = list(map(int, input().split()))
for i in a:
	tmp = (i ** 4 - k * i) % p
	if tmp not in d:
		d[tmp] = 1
	else:
		d[tmp] += 1
ans = 0
for i in d:
	ans += d[i] * (d[i] - 1) // 2
print(ans)


(n, p, k) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
counter = 0
b = {}
for item in a:
	x = (item ** 4 - k * item) % p
	if x not in b:
		b[x] = 1
	else:
		b[x] += 1
for item in b:
	counter += b[item] * (b[item] - 1) // 2
print(counter)