$P(n)$ and $D(n)$ are two large integers.

Suppose $R(n) = \frac{P(n)}{D(n)}$ is an integer.

I want to compute $R(n)\bmod m$.

$P(n)$ and $D(n)$ are too large to be computed but $P(n)\bmod m$ and $D(n)\bmod m$ can easily be computed.

For example, how to compute $S(x,k) = \sum_{k=0}^{n}x^k\mod m$ for a large $n$?

$S(x,n) = \frac{x^{n+1}-1}{x-1}$

I know how to do if $x-1$ and $m$ are coprime, using the modular inverse of $x-1$, but what if they are not coprime?

Thanks in advance

Philippe