Hi @jbhv12 ! I modified your solution and submitted. It got an AC. See here.
Mistakes i found in your code:-
Instead of power you used inbuilt pow function many times. That would obviously result in overflow and give wrong value. I replaced every pow with your power().
I replaced your following line
ll p = (pow(n,y+1)) - (n*pow(n-1,y)) + (pow(n-1,y));
p -= (n*power(n-1,y, mod))%mod;
p += mod;
p %= mod;
p += (power(n-1,y, mod))%mod;
p %= mod;```
Because whenever you subtract something then you may get negative value so taking modulo will also result in negative answer. So always do ```ans = (ans+mod)%mod```.
3) I replaced your line
```ll p1 = a*q + p*b;```
with ```ll p1 = (a*q)%mod + (p*b)%mod;```
4) and put %mod on almost every step.
Hooray! It resulted in AC.
General Tips for such questions:
1) Always use modulo power exponentiation whenever answer is asked to compute modulo M.
2) Whenever ans becomes negative(either by subtracting or by multiplying with negative number) use ans = (ans%MOD + MOD) % MOD as suggested by @l_returns
3) Use ```%MOD``` after each arithmetic operation.