All the numbers that are coprime to n have a multiplicative inverse - you don’t need to find the actual inverses. See Euler’s totient function \phi(n) to find out how many there are of these.

This function is multiplicative, and given the restriction on n, it is quick to assess whether each of the primes (2,3,5,7,11,13) divide n and adjust accordingly. For example if n is a power of 7 you just need to multiply by 6/7 to find \phi(n).

Not giving full code, but the process is: Starting with ans = n, we can check whether n is divisible by each of the above primes p in turn (e.g. if 0 == n%p) and if so set ans = (p-1) * (ans / p)