If you can find Wilson's Theorem you can solve this problem, also you should brush up on your Modulo Multiplicative Inverse to understand what's going on here
All that is left is to generate all primes up till 10^8 using my Prime Generator function, this takes ~30 seconds, the rest of the code takes ~7, and loop through them.
In python 3.8+ you can simply do pow(8, -1, p), however if you're using a lower version you can use Euler's Theorem
Enter an integer (yourinput) (Choose a number < 10^7 because prime generation takes a while)
Code will output the sum of S(p) for 5 <= p <= yourinput