My ans is 2nd or 3rd minimum value

take modulo after finding x at last and use long long for x you will get AC and divide prod by num not inverse.
see code for details

thanks a lot sir for your help

can you explain the approach of solving this question