It is quite clear that if a≡c and b≡d (mod p), then ab≡cd (mod p), or if a≡d and b≡c (mod p). There are p cases of a≡c (mod p) as well as for b≡d (mod p), and so there are a total of p^2 cases of a≡c and b≡d (mod p). This is also the case for a≡d and b≡c (mod p), which gives together 2p^2-1 solutions, removing the case where a≡b≡c≡d (mod p), as it is repeated. There is also the case where (a or b) ≡ p (mod p) together with (c or d) ≡ p (mod p). However there do exist cases of ab≡cd (mod p) where neither a nor b is congruent to c nor d (mod p). An example of this is 23*56≡3*37≡4 (mod 107).

I do not know of any method to account for all of these cases. And so, how can I approach finding the number of solutions to ab≡cd (mod p) where p is a prime and 1≤a,b,c,d≤p?

Thank you very much.