Why am I getting TLE
Question link
Submission link
My solution is in O(n^2*log(mod)) . It should pass easily