extgcd(a, m) finds x, y with a·x + m·y = gcd(a, m). When gcd = 1, the Bezout x is the modular inverse a−1 mod m.
Each row of the iteration already carries the identity r = s·a + t·m.
We replay them from the top so you can watch the remainder shrink while the
coefficients s and
t grow toward the final Bezout pair.
Once r = gcd, those coefficients are
x and y.
r = s·a + t·b: each row stores (r, s, t) so when r reaches 0 the previous row is (gcd, x, y).
The inverse exists iff gcd(a, m) = 1. See junior.md and professional.md for the proofs.