using
System;
using
System.Collections.Generic;
class
Pair
{
public
int
First {
get
;
set
; }
public
int
Second {
get
;
set
; }
public
Pair(
int
first,
int
second)
{
First = first;
Second = second;
}
}
class
GFG
{
static
long
Pow(
long
a,
long
b,
long
m)
{
if
(b == 0)
return
1;
long
temp = 1;
for
(
long
current = a; b != 0; b >>= 1, current = current * current % m)
{
if
((b & 1) == 1)
temp = temp * current % m;
}
return
temp;
}
static
void
GetValue(
int
x,
int
y,
int
z)
{
List<Pair> v =
new
List<Pair>();
for
(
int
i = 1, current = 1; i <= y; i <<= 1)
{
v.Add(
new
Pair(current, i));
current = (
int
)((
long
)current * (Pow(x, i, z) + 1) % z);
}
int
ans = 0, res = 0;
while
(v.Count > 0)
{
Pair temp = v[v.Count - 1];
v.RemoveAt(v.Count - 1);
if
(y < temp.Second)
continue
;
ans = (
int
)((
long
)ans + Pow(x, res, z) * temp.First % z) % z;
res += temp.Second;
y -= temp.Second;
}
Console.WriteLine(ans);
}
static
void
Main(
string
[] args)
{
int
x = 20000, y = 35000, z = 2555;
GetValue(x, y, z);
}
}