I am just starting with competitive programing and usually numbers get way too large so we tend to work with $$ \mod 10^9+7$$
I am familiar with some properties of modulo like,
$$(a+b) \mod c = (a\mod c+b\mod c) \mod c$$ $$(a-b) \mod c = (a\mod c-b\mod c) \mod c$$ $$(a*b) \mod c = (a\mod c*b\mod c) \mod c$$
But recently I stumbled upon this formula,
$$y=\frac{4a^3-a^2}{3}$$
This is the Faulhaber Formula for $\sum_{i=0}^{n}{i^5}$, where $a=\sum_{i=0}^{n}{i}$.
Now this is what has me stuck.
In my scenario $a\approx10^{16}$ and the largest value I can store is $\approx 10^{19}$.
and I want to evaluate,
$$\frac{4a^3-a^2}{3} \mod 10^9+7$$
Clearly I cannot calculate $a^3$ due to overflow also I am finding it hard to distribute the modulo because of the $3$ in the denominator. How do I get around this?
Any suggestions will be helpful. Thanks.
We can use
$$a^3 \pmod{p} \equiv \color{red}[\color{blue}[(a \pmod{p}) \cdot (a \pmod{p})\pmod{p}\color{blue}] \cdot (a \pmod{p}) \color{red}]\pmod{p}$$
That is we keep computing modulo after each step of computation, hence the number stays within the limit of your computation.