The congruence pr≡1(modn!) can be broken down into a system of congruences for each prime power factor of n!. Let the prime factorization of n! be n!=∏q≤nqvq(n!), where q are primes and vq(n!) is the exponent of q in the factorization, given by Legendre’s formula: vq(n!)=∑k=1∞⌊n/qk⌋.
By the Chinese Remainder Theorem, the congruence is equivalent to the system:
pr≡1(modqvq(n!))for all primes q≤n
For this system to hold, r must be a multiple of the multiplicative order of p modulo each prime power qvq(n!). The minimal such positive r is therefore the least common multiple (LCM) of these orders.
The core of the problem is to find ordqa(p), the order of p modulo a prime power qa. This can be determined from the order modulo q using a principle often called the “Lifting The Exponent Lemma for Orders”.
For odd primes q:
Let k=ordq(p). Then ordqa(p)=k⋅qa−c, where c=vq(pk−1).
For the prime q=2:
Let k=ord2(p)=1. Then ord2a(p) depends on v2(p2−1). Specifically, for a≥c=v2(p2−1), the order is ord2a(p)=2⋅2a−c=2a−c+1.
The prime factorization of the answer R(p,n) is ∏ses. The exponent es for each prime s is the maximum power of s found among all the orders ordqvq(n!)(p).
es=q≤nmax{vs(ordqvq(n!)(p))}
Analysis shows that for a large n, the term vs(n!) is significantly larger than any other contributing factor. This leads to a major simplification: the exponent es is dominated by the case where s=q.
The term vs(pords(p)−1) is almost always 1. A value greater than 1 occurs only if pords(p)≡1(mods2), which implies ps−1≡1(mods2). Primes s with this property (for a given base p) are called Wieferich primes and are exceptionally rare.
We are given p=109+7 and n=107. We can make the standard and safe assumption that for p=109+7, no odd Wieferich primes exist up to 107.
For odd primes q≤n: We assume vq(pordq(p)−1)=1. The exponent of q in the final answer is vq(n!)−1.
For the prime q=2: We must calculate c2=v2(p2−1).
p=109+7≡7(mod8).
p−1=109+6, so v2(p−1)=1.
p+1=109+8, so v2(p+1)=3.
c2=v2(p2−1)=v2(p−1)+v2(p+1)=1+3=4.
The exponent of 2 in the answer is v2(n!)−4+1=v2(n!)−3.
Combining these results, the final answer A=R(p,n) is: