For prime M, the modular inverse of A{-}1 can be found from b^{M-1} \equiv 1 \bmod M, giving b\cdot b^{M-2} \equiv 1 \bmod M and thus b^{-1} \equiv b^{M-2} \bmod M. Quicker, more general purpose but harder to explain is theextended Euclidean algorithm.
You can use the formula for geometric sum if the modular multiplicative inverse of the denominator of the geometric sum exists. Refer to @joffan 's answer.
Otherwise, you can solve this recursively. Note that:
That’s a good solution also, for cases where (A-1) has no modular inverse. However even for non-prime M, whenever M and (A-1) have no common factors, the modular inverse does exist. In that case you can use the extended Euclidean algorithm to find the modular inverse of (A-1) \bmod M - and that algorithm can also tell you whether the inverse exists.