Modular Inverses

Suppose you want to solve 7x≡1(mod31)7x \equiv 1 \pmod{31}. Is there an integer xx that plays the role of "1/71/7"? This is true in the world of remainders modulo 31. This is the question of modular inverses. It turns out to have a remarkably clean answer. Division is the one arithmetic operation that fails to survive intact. This happens when we pass from ordinary integers to residues modulo nn. Understanding exactly when it survives — and how to recover it — is one of the foundational pillars on which the rest of elementary number theory is built. The idea matters because so much of number theory, cryptography, and computer algebra is secretly about solving equations inside Z/nZ\mathbb{Z}/n\mathbb{Z}. Every time RSA encryption decrypts a message, the same question lurks underneath. Every time a computer algebra system solves a linear congruence, it lurks too. Every time a hash table probes indices modulo its size, the question reappears. For which aa does there exist a multiplicative partner bb with ab≡1ab \equiv 1? And how do we find it fast? The answer — exactly when gcd⁡(a,n)=1\gcd(a,n)=1 — is short to state. But it is rich in consequence. It connects Bezout's Identity, The Extended Euclidean Algorithm, and the structure of (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^{\times} as a group. The big picture is that modular arithmetic modulo nn forms a ring. The units of that ring have multiplicative inverses. These units form a group under multiplication. Classifying the units of Z/nZ\mathbb{Z}/n\mathbb{Z} is the first step toward Euler's theorem. It also leads toward Fermat's little theorem, and the notion of the Order of an Element. Eventually it connects to the structure theory of finite abelian groups applied to number theory. Without inverses we cannot even meaningfully "divide" in modular arithmetic. So solving Linear Congruences of the form ax≡b(modn)ax \equiv b \pmod n reduces almost entirely to first finding an inverse of aa. This article gives the precise definition of a modular inverse. It proves the fundamental existence theorem: invertibility holds exactly when gcd⁡(a,n)=1\gcd(a,n)=1. It gives a full algorithmic account of how to compute inverses via the extended Euclidean algorithm. It works two nontrivial numerical examples. Finally, it examines exactly what goes wrong when the coprimality hypothesis fails.

Key takeaways
  • An inverse of aa modulo nn is a residue bb with ab≡1(modn)ab \equiv 1 \pmod n; when it exists it is unique modulo nn.
  • The inverse of aa mod nn exists if and only if gcd⁡(a,n)=1\gcd(a,n) = 1.
  • Existence is proved via Bezout's Identity: gcd⁡(a,n)=1\gcd(a,n)=1 gives integers x,yx,y with ax+ny=1ax+ny=1, so xx is the inverse.
  • The Extended Euclidean Algorithm computes this xx efficiently, in O(log⁡n)O(\log n) arithmetic steps.
  • The invertible residues mod nn form a group under multiplication, denoted (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^{\times}, of order φ(n)\varphi(n).
  • Key idea: modular division by aa is well-defined precisely on the units of Z/nZ\mathbb{Z}/n\mathbb{Z}, and Bezout's identity turns the abstract existence statement into an explicit, computable inverse.

Where this lives today

Modular inverses are the computational backbone of RSA and elliptic-curve cryptography: key generation and decryption both require computing an inverse modulo a large composite or prime, almost always via the extended Euclidean algorithm rather than brute force, because brute force is infeasible for numbers with hundreds of digits. In error-correcting codes and finite-field arithmetic (e.g. Reed–Solomon codes), inverses in Fp\mathbb{F}_p or Fpk\mathbb{F}_{p^k} are needed at every decoding step. In computer algebra systems, solving linear congruences and systems of congruences via the Chinese Remainder Theorem reduces to computing inverses in each modulus. In hashing and pseudorandom number generation, invertibility modulo the table size or modulus ensures certain linear maps remain bijective, which is essential for full-period linear congruential generators. The same existence criterion also foreshadows The Order of an Element in (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^{\times}, Wilson's Theorem's use of inverse-pairing among residues modulo a prime, and general Linear Congruences theory, where solving ax≡b(modn)ax \equiv b \pmod n reduces immediately to multiplying both sides by a−1a^{-1} whenever it exists.

Ask the tutor: How does the extended Euclidean algorithm's back-substitution actually produce the coefficients x,yx, y in Bezout's identity, step by step, for a modulus of my choosing?

DefinitionModular Inverse

Let nn be a positive integer and let aa be an integer. A modular inverse of aa modulo nn is an integer bb such that

ab≡1(modn),ab \equiv 1 \pmod n,

that is, n∣(ab−1)n \mid (ab - 1). If such a bb exists, we say aa is invertible (or a unit) modulo nn, and we write b≡a−1(modn)b \equiv a^{-1} \pmod n. Because ab≡1(modn)ab \equiv 1 \pmod n depends only on the residue classes of aa and bb modulo nn, invertibility and the inverse (when it exists) are properties of the residue class a  na \bmod n, not of the particular representative integer aa. The set of invertible residue classes modulo nn, with multiplication induced from Z\mathbb{Z}, is denoted

(Z/nZ)×={ [a]∈Z/nZ:gcd⁡(a,n)=1 }(\mathbb{Z}/n\mathbb{Z})^{\times} = \{\, [a] \in \mathbb{Z}/n\mathbb{Z} : \gcd(a,n) = 1 \,\}

(the equality of this set with the units will be exactly the content of the theorem below).

Intuition

Ordinary division by a nonzero integer aa is a global operation on Q\mathbb{Q}: every nonzero rational number has a reciprocal. Modular arithmetic is different because Z/nZ\mathbb{Z}/n\mathbb{Z} is a finite ring with zero divisors when nn is composite — for instance 2⋅3≡0(mod6)2 \cdot 3 \equiv 0 \pmod 6, yet neither 22 nor 33 is 00. In such a ring, not every nonzero element can have a reciprocal, since if aa had an inverse bb and ac≡0ac \equiv 0 with c≢0c \not\equiv 0, multiplying by bb would force c≡0c \equiv 0, a contradiction. So zero divisors are never invertible, and the question becomes: which residues escape being zero divisors entirely? The answer is visible already in the structure of the complete residue system {0,1,…,n−1}\{0, 1, \dots, n-1\}. Picture multiplication by aa as a function on this set. If gcd⁡(a,n)=d>1\gcd(a,n) = d > 1, then every multiple of aa modulo nn is automatically a multiple of dd (since aa is), so the map x↦ax  nx \mapsto ax \bmod n can never hit 11 when d∤1d \nmid 1. Coprimality removes this obstruction entirely, and Bezout's identity shows the obstruction is the only one: the linear combinations ax+nyax + ny range exactly over the multiples of gcd⁡(a,n)\gcd(a,n), so 11 is achievable precisely when gcd⁡(a,n)=1\gcd(a,n)=1.

Figure
Multiplication-by-aa as a permutation of residues, contrasted with a non-invertible case
mod 9, a = 2 (gcd(2,9)=1): bijection, inverse exists (2·5≡1) x: 0 1 2 3 4 5 6 7 8 2x mod 9: 0 2 4 6 8 1 3 5 7 mod 9, a = 3 (gcd(3,9)=3): not injective, 1 never hit x: 0 1 2 3 4 5 6 7 8 3x mod 9: 0 3 6 0 3 6 0 3 6 Range of 3x mod 9 is only {0,3,6}: never equals 1, so 3 has no inverse mod 9.

Intuition (continued): the Euclidean algorithm as a search for the inverse

The extended Euclidean algorithm gives more than an abstract existence proof: it is an efficient constructive method. Running the Euclidean algorithm on (a,n)(a, n) backward through its sequence of remainders expresses gcd⁡(a,n)\gcd(a,n) as an integer combination ax+nyax + ny, and reducing xx modulo nn produces the inverse directly — no search over {0,…,n−1}\{0, \dots, n-1\} required.

TheoremExistence and Uniqueness of Modular Inverses

Let nn be a positive integer and aa an integer. Then: 1. (Existence) aa has a modular inverse modulo nn if and only if gcd⁡(a,n)=1\gcd(a,n) = 1. 2. (Uniqueness) If aa has an inverse modulo nn, it is unique modulo nn: any two inverses b1,b2b_1, b_2 satisfy b1≡b2(modn)b_1 \equiv b_2 \pmod n.

Proof

Existence, (⇐\Leftarrow). Suppose gcd⁡(a,n)=1\gcd(a,n) = 1. By Bezout's Identity, there exist integers x,yx, y such that

ax+ny=1.ax + ny = 1.

Reducing this equation modulo nn eliminates the term nyny, since n∣nyn \mid ny, giving

ax≡1(modn).ax \equiv 1 \pmod n.

Hence xx is a modular inverse of aa modulo nn; existence is proved, and the proof is constructive once we recall that Bezout's identity itself is proved constructively via the Extended Euclidean Algorithm.

Existence, (⇒\Rightarrow). Suppose aa has a modular inverse bb modulo nn, so ab≡1(modn)ab \equiv 1 \pmod n. By definition of congruence this means n∣(ab−1)n \mid (ab - 1), i.e. there is an integer kk with

ab−1=nk,equivalentlyab−nk=1.ab - 1 = nk, \qquad \text{equivalently} \qquad ab - nk = 1.

Let d=gcd⁡(a,n)d = \gcd(a,n). Since d∣ad \mid a and d∣nd \mid n, dd divides every integer linear combination of aa and nn; in particular d∣(ab−nk)=1d \mid (ab - nk) = 1. As dd is a positive integer dividing 11, we conclude d=1d = 1, i.e. gcd⁡(a,n)=1\gcd(a,n) = 1. This proves the biconditional in (1): the direction (⇒)(\Rightarrow) shows coprimality is necessary, and (⇐)(\Leftarrow) shows it is sufficient, and neither direction invokes the theorem itself, so the argument is non-circular.

Uniqueness. Suppose b1b_1 and b2b_2 are both modular inverses of aa modulo nn, so

ab1≡1(modn)andab2≡1(modn).ab_1 \equiv 1 \pmod n \qquad \text{and} \qquad ab_2 \equiv 1 \pmod n.

Subtracting the two congruences (permissible since congruence modulo nn is compatible with addition and subtraction) gives

a(b1−b2)≡0(modn),a(b_1 - b_2) \equiv 0 \pmod n,

that is, n∣a(b1−b2)n \mid a(b_1 - b_2). By part (1) (already established), since ab1≡1(modn)ab_1 \equiv 1 \pmod n we know gcd⁡(a,n)=1\gcd(a,n) = 1. A standard consequence of gcd⁡(a,n)=1\gcd(a,n)=1 together with n∣a(b1−b2)n \mid a(b_1-b_2) is n∣(b1−b2)n \mid (b_1 - b_2): indeed, by Bezout's identity there are integers u,vu,v with au+nv=1au + nv = 1, so

b1−b2=(au+nv)(b1−b2)=u(a(b1−b2))+nv(b1−b2),b_1 - b_2 = (au+nv)(b_1-b_2) = u\big(a(b_1-b_2)\big) + nv(b_1-b_2),

and both terms on the right are divisible by nn (the first because n∣a(b1−b2)n \mid a(b_1-b_2), the second visibly), so n∣(b1−b2)n \mid (b_1-b_2). Hence b1≡b2(modn)b_1 \equiv b_2 \pmod n, proving uniqueness. ■\blacksquare

∎
CorollaryThe Group of Units

The set of residue classes modulo nn that possess a multiplicative inverse is exactly

(Z/nZ)×={ [a]:1≤a≤n, gcd⁡(a,n)=1 },(\mathbb{Z}/n\mathbb{Z})^{\times} = \{\, [a] : 1 \le a \le n,\ \gcd(a,n)=1 \,\},

and this set, with multiplication induced from Z\mathbb{Z}, forms an abelian group of order φ(n)\varphi(n), where φ\varphi is Euler's totient function.

Proof

By the theorem, [a][a] has an inverse exactly when gcd⁡(a,n)=1\gcd(a,n) = 1, so the two descriptions of the set coincide; its cardinality is φ(n)\varphi(n) by the definition of Euler's totient function. We check the group axioms. Closure: if gcd⁡(a,n)=gcd⁡(b,n)=1\gcd(a,n) = \gcd(b,n) = 1 then gcd⁡(ab,n)=1\gcd(ab, n) = 1 (a prime p∣np \mid n dividing abab would have to divide aa or bb, contradicting one of the coprimality assumptions), so abab is again a unit. Associativity and commutativity are inherited from multiplication in Z\mathbb{Z}, which descends to well-defined, associative, commutative multiplication on residue classes. Identity: [1][1] is a unit and acts as identity. Inverses: by the theorem, every element of the set has an inverse, and that inverse is itself coprime to nn (if ab≡1ab \equiv 1 then any common factor of bb and nn would divide ab−1+1=abab - 1 + 1= ab hence force issues; more directly, bb is a unit by the same theorem applied to bb, since ba≡1ba \equiv 1 exhibits aa as an inverse of bb), so inverses stay inside the set. All group axioms hold. ■\blacksquare

∎

Worked examples

Example 1: Inverse of 17 modulo 43. First confirm gcd⁡(17,43)=1\gcd(17, 43) = 1. Since 4343 is prime and 17≠4317 \ne 43, this is immediate, but let us verify with the Euclidean algorithm, since we need its steps anyway:

43=2⋅17+943 = 2 \cdot 17 + 9
17=1⋅9+817 = 1 \cdot 9 + 8
9=1⋅8+19 = 1 \cdot 8 + 1
8=8⋅1+0.8 = 8 \cdot 1 + 0.

The last nonzero remainder is 11, confirming gcd⁡(17,43)=1\gcd(17,43) = 1, so an inverse exists. Now back-substitute to write 11 as an integer combination of 1717 and 4343:

1=9−1⋅81 = 9 - 1\cdot 8
=9−1⋅(17−1⋅9)=2⋅9−1⋅17= 9 - 1\cdot(17 - 1\cdot 9) = 2\cdot 9 - 1\cdot 17
=2⋅(43−2⋅17)−1⋅17=2⋅43−5⋅17.= 2\cdot(43 - 2\cdot 17) - 1\cdot 17 = 2\cdot 43 - 5\cdot 17.

So

17⋅(−5)+43⋅2=1,17\cdot(-5) + 43\cdot 2 = 1,

which reads 17⋅(−5)≡1(mod43)17 \cdot (-5) \equiv 1 \pmod{43}. Reducing −5-5 modulo 4343 gives −5+43=38-5 + 43 = 38. So the inverse is

17−1≡38(mod43).17^{-1} \equiv 38 \pmod{43}.

Check: 17×38=64617 \times 38 = 646, and 646=15×43+1646 = 15 \times 43 + 1 since 15×43=64515 \times 43 = 645. Indeed 646−645=1646 - 645 = 1, confirming 17×38≡1(mod43)17 \times 38 \equiv 1 \pmod{43}.

Example 2: Inverse of 24 modulo 91 — a case requiring the full algorithm on composite modulus. Here 91=7×1391 = 7 \times 13. Check gcd⁡(24,91)\gcd(24, 91) via Euclid:

91=3⋅24+1991 = 3 \cdot 24 + 19
24=1⋅19+524 = 1 \cdot 19 + 5
19=3⋅5+419 = 3 \cdot 5 + 4
5=1⋅4+15 = 1 \cdot 4 + 1
4=4⋅1+0.4 = 4 \cdot 1 + 0.

Last nonzero remainder is 11, so gcd⁡(24,91)=1\gcd(24, 91) = 1 and the inverse exists (this also reconfirms independently that 2424 shares no factor with either 77 or 1313). Back-substitute:

1=5−1⋅41 = 5 - 1\cdot 4
=5−1⋅(19−3⋅5)=4⋅5−1⋅19= 5 - 1\cdot(19 - 3\cdot 5) = 4\cdot 5 - 1\cdot 19
=4⋅(24−1⋅19)−1⋅19=4⋅24−5⋅19= 4\cdot(24 - 1\cdot 19) - 1\cdot 19 = 4\cdot 24 - 5\cdot 19
=4⋅24−5⋅(91−3⋅24)=19⋅24−5⋅91.= 4\cdot 24 - 5\cdot(91 - 3\cdot 24) = 19\cdot 24 - 5\cdot 91.

So 24⋅19−91⋅5=124 \cdot 19 - 91\cdot 5 = 1, giving

24−1≡19(mod91).24^{-1} \equiv 19 \pmod{91}.

Check: 24×19=45624 \times 19 = 456. Divide: 456=5×91+1456 = 5\times 91 + 1, since 5×91=4555 \times 91 = 455. So 456−455=1456 - 455 = 1, confirming 24×19≡1(mod91)24 \times 19 \equiv 1 \pmod{91}.

Figure
Back-substitution unwinding the Euclidean algorithm for 24 and 91
Forward (Euclid): 91=3·24+19, 24=1·19+5, 19=3·5+4, 5=1·4+1 Backward (Bezout), substituting each remainder in turn: 1 = 5 − 1·4 1 = 4·5 − 1·19 1 = 4·24 − 5·19 1 = 19·24 − 5·91 so 24⁻¹ ≡ 19 (mod 91) Check: 24×19 = 456 = 5×91 + 1 ✓
CounterexampleWhy coprimality cannot be dropped

Consider a=6a = 6 and n=15n = 15, so gcd⁡(6,15)=3≠1\gcd(6,15) = 3 \ne 1. The theorem's necessity direction predicts no inverse exists; let us verify this directly by brute force, confirming there is no gap. Compute 6b  156b \bmod 15 for every residue b∈{0,1,…,14}b \in \{0,1,\dots,14\}:

6⋅0=0, 6⋅1=6, 6⋅2=12, 6⋅3=18≡3, 6⋅4=24≡9, 6⋅5=30≡0,6\cdot0=0,\ 6\cdot1=6,\ 6\cdot2=12,\ 6\cdot3=18\equiv3,\ 6\cdot4=24\equiv9,\ 6\cdot5=30\equiv0,
6⋅6=36≡6, 6⋅7=42≡12, 6⋅8=48≡3, 6⋅9=54≡9, 6⋅10=60≡0,6\cdot6=36\equiv6,\ 6\cdot7=42\equiv12,\ 6\cdot8=48\equiv3,\ 6\cdot9=54\equiv9,\ 6\cdot10=60\equiv0,
6⋅11=66≡6, 6⋅12=72≡12, 6⋅13=78≡3, 6⋅14=84≡9.6\cdot11=66\equiv6,\ 6\cdot12=72\equiv12,\ 6\cdot13=78\equiv3,\ 6\cdot14=84\equiv9.

The complete list of values attained is {0,3,6,9,12}\{0, 3, 6, 9, 12\} — exactly the multiples of 3=gcd⁡(6,15)3 = \gcd(6,15) inside {0,…,14}\{0,\dots,14\} — and 11 never appears. So no bb satisfies 6b≡1(mod15)6b \equiv 1 \pmod{15}: 66 has no inverse modulo 1515. This is exactly what the (⇒\Rightarrow) direction of the theorem's proof predicts: if an inverse bb existed, then gcd⁡(6,15)=3\gcd(6,15) = 3 would have to divide 6b−16b - 1's complement, forcing 3∣13 \mid 1, which is false. Dropping the hypothesis gcd⁡(a,n)=1\gcd(a,n)=1 genuinely breaks the conclusion — it is not a vacuous or a technical assumption, but the precise dividing line between residues that are units and residues that are zero divisors. Note also that 66 is a zero divisor modulo 1515: 6×5=30≡0(mod15)6 \times 5 = 30 \equiv 0 \pmod {15} with 5≢05 \not\equiv 0, consistent with the general fact that zero divisors are never invertible.

Scope, edge cases & common traps

SituationWhat happensWhy
gcd⁡(a,n)=1\gcd(a,n) = 1Unique inverse mod nn existsBezout's identity, theorem above
gcd⁡(a,n)=d>1\gcd(a,n) = d > 1No inverse existsd∣(ab−1)d \mid (ab-1) forces d=1d=1, contradiction
nn prime, a≢0a \not\equiv 0Inverse always existsgcd⁡(a,n)=1\gcd(a,n)=1 automatic since nn prime
a≡0(modn)a \equiv 0 \pmod n, n>1n>1No inverse (special case of d=n>1d=n>1)0⋅b≡0≢10 \cdot b \equiv 0 \not\equiv 1
Working with negative aaReduce a  na \bmod n first; theorem is stated for the residue classCongruence only depends on residue class
n=1n = 1Every integer is "invertible" trivially, since all integers are ≡0≡1(mod1)\equiv 0 \equiv 1 \pmod 1Degenerate ring with one element

The mathematician behind it

Origins · 1 minÉtienne Bézout and how this was discovered
ÉB
Étienne Bézout
1730–1783 · French mathematician, Académie des Sciences

Bézout is best remembered for Bézout's identity and Bézout's theorem in algebraic geometry, both expressing the same intuition: that the "size" of an intersection or combination is governed by a greatest common divisor or a product of degrees. His systematic study, in the 1770s, of the integer solvability of ax+by=cax+by=c gave the theoretical machinery — later streamlined via the Euclidean algorithm — that makes the existence of modular inverses a one-line consequence rather than a case-by-case verification.

History
- c. 300 BCE — Euclid's Elements Book VII describes the algorithm for the greatest common divisor that underlies all later computation of modular inverses.
- 1621 — Bachet de Méziriac publishes a coefficient-finding procedure equivalent to solving ax+by=gcd⁡(a,b)ax+by=\gcd(a,b), anticipating Bezout's identity.
- 1770s — Étienne Bézout formalizes and generalizes the identity bearing his name for integer (and polynomial) linear combinations.
- 1801 — Gauss's Disquisitiones Arithmeticae systematizes congruence notation and treats invertible residues as the foundation of modular arithmetic.
- 20th century — the extended Euclidean algorithm becomes a core primitive in computational number theory and cryptography, used explicitly to compute modular inverses at scale.
Does every nonzero residue modulo nn have an inverse?

Only when nn is prime. If nn is prime, every aa with 1≤a≤n−11 \le a \le n-1 automatically satisfies gcd⁡(a,n)=1\gcd(a,n)=1, since nn's only divisors are 11 and nn itself, and a<na<n rules out nn. So modulo a prime, every nonzero residue is invertible, and (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^{\times} has all p−1p-1 nonzero classes as units. For composite nn, this fails: any aa sharing a common prime factor with nn is a zero divisor and has no inverse, as shown in the counterexample with 66 modulo 1515.

How do I actually compute a modular inverse by hand efficiently?

Run the Euclidean algorithm on (a,n)(a,n) to obtain a sequence of quotients and remainders down to gcd⁡(a,n)=1\gcd(a,n)=1, then back-substitute from the last nontrivial step upward to express 1=ax+ny1 = ax + ny for some integers x,yx,y. The residue of xx modulo nn is the inverse. This is the extended Euclidean algorithm, and it runs in time proportional to the number of steps in the ordinary Euclidean algorithm, which is O(log⁡min⁡(a,n))O(\log \min(a,n)) divisions — dramatically faster than testing every residue.

Is the modular inverse the same as 1/a1/a as a real number reduced mod nn?

No — there is no meaningful reduction of the real number 1/a1/a modulo nn since 1/a1/a typically is not an integer. The modular inverse is a genuinely different integer bb, found by solving ab≡1(modn)ab \equiv 1 \pmod n, and it has nothing to do with rounding or truncating the real reciprocal. For instance 7−1≡15(mod26)7^{-1} \equiv 15 \pmod{26}, which bears no numerical resemblance to 1/7≈0.1428571/7 \approx 0.142857.

What happens if I try to invert 0 modulo n, for n > 1?

It never has an inverse. Since 0⋅b≡0(modn)0 \cdot b \equiv 0 \pmod n for every bb, and 0≢1(modn)0 \not\equiv 1 \pmod n whenever n>1n > 1, no bb can satisfy 0⋅b≡10\cdot b\equiv 1. This matches the theorem: gcd⁡(0,n)=n\gcd(0,n) = n, which equals 11 only when n=1n=1, a degenerate modulus where every congruence is trivially true.

Can an element have more than one inverse modulo n?

No, and this is the content of the uniqueness half of the theorem. If b1b_1 and b2b_2 both invert aa modulo nn, then a(b1−b2)≡0(modn)a(b_1-b_2) \equiv 0 \pmod n, and because gcd⁡(a,n)=1\gcd(a,n)=1 this forces n∣(b1−b2)n \mid (b_1-b_2), i.e. b1≡b2(modn)b_1\equiv b_2 \pmod n. So while there are infinitely many integers representing the inverse (e.g. …,−8,38,81,…\dots, -8, 38, 81,\dots all represent the same class modulo 4343 in Example 1), there is exactly one residue class.

How does this relate to solving general linear congruences ax≡b(modn)ax \equiv b \pmod n?

Once you know a−1a^{-1} exists (i.e., gcd⁡(a,n)=1\gcd(a,n)=1), you solve ax≡b(modn)ax\equiv b\pmod n by multiplying both sides by a−1a^{-1}, giving the unique solution x≡a−1b(modn)x \equiv a^{-1}b \pmod n. When gcd⁡(a,n)=d>1\gcd(a,n) = d > 1, the congruence needs a separate, more general treatment: it has solutions only if d∣bd \mid b, and then there are exactly dd solutions modulo nn. That fuller theory is the subject of Linear Congruences, and it builds directly on the existence criterion proved here.

Why is coprimality with n, rather than with any particular prime factor, the right condition?

Because gcd⁡(a,n)=1\gcd(a,n)=1 is exactly equivalent to aa sharing no prime factor with nn at all — if aa shared even one prime factor pp of nn, that pp would divide gcd⁡(a,n)\gcd(a,n), blocking invertibility, as the necessity proof shows via divisibility of ab−1ab-1. Conversely, having no shared prime factor is precisely what Bezout's identity requires and delivers: gcd⁡(a,n)=1\gcd(a,n)=1 is the necessary and sufficient condition, not merely coprimality with some factor or most factors.

Before this

Where this leads