Given X,p,a,b, we need to find out how many n\in\mathbb N,\;(1\leqslant n\leqslant X)\; satisfy the following condition:

na^n\equiv b\pmod{p}

Constraints:

(1\leqslant X \leqslant 10^{12})\;

(1\leqslant p \leqslant 10^{6})\;

(1\leqslant a,b \leqslant p)\;

I have no idea how to solve this question, any approach or proof will be highly appreciated.

Thank you in advance!