Houraisan Kaguya is fighting against Fujiwara no Mokou over the moon. Suddenly, Mokou launches a spell “Imperishable Shooting”(just a programming problem, believe it or not) to attack Kaguya, which is as follows.
Given a prime number p and n positive integers a1, a2, · · · , an which are strictly less than p.
For two integers a, b (0 ≤ a, b < p), we say a is representable by b if and only if there exists a positive integer x that bx ≡ a(mod p). Furthermore, we define f(a, b) as the minimum positive integer u that au modulo p is representable by b. If no such u exists, f(a, b) is specially defined as 0.
The problem is to determine the value of following formula.
$$(\sum_{i=1}^{n}\sum_{j=1}^{n}f(a_i,a_j)\times f(a_j,a_i)) \mod p$$
Please help Kaguya solve it so that Kaguya can give Mokou the sixth puzzle in the next round.