```go
package main
import (
"fmt"
"io"
"os"
)
func skipSpace(bytes []byte, i int) int {
for i < len(bytes) && bytes[i] <= ' ' {
i++
}
return i
}
func main() {
bytes, _ := io.ReadAll(os.Stdin)
i := skipSpace(bytes, 0)
if i >= len(bytes) {
return
}
startB := i
for i < len(bytes) && bytes[i] > ' ' {
i++
}
endB := i
i = skipSpace(bytes, i)
startN := i
for i < len(bytes) && bytes[i] > ' ' {
i++
}
endN := i
i = skipSpace(bytes, i)
var c uint64
for i < len(bytes) && bytes[i] > ' ' {
c = c*10 + uint64(bytes[i]-'0')
i++
}
if c == 1 {
fmt.Println(1)
return
}
var B uint64
for _, ch := range bytes[startB:endB] {
B = (B*10 + uint64(ch-'0')) % c
}
B1 := (B + c - 1) % c
nBytes := bytes[startN:endN]
for j := len(nBytes) - 1; j >= 0; j-- {
if nBytes[j] > '0' {
nBytes[j]--
break
} else {
nBytes[j] = '9'
}
}
P := uint64(1)
for _, ch := range nBytes {
d := uint64(ch - '0')
P1 := uint64(1)
for j := 0; j < 10; j++ {
P1 = (P1 * P) % c
}
P2 := uint64(1)
for j := uint64(0); j < d; j++ {
P2 = (P2 * B) % c
}
P = (P1 * P2) % c
}
ans := (B1 * P) % c
if ans == 0 {
ans = c
}
fmt.Println(ans)
}
```