Sum of a geometric sequence

Add the first n terms of a geometric sequence with first term a and ratio r, then print the sum modulo mod.

Medium5Divide and conquerMathRecursionNo attempts yetTime limit0.5sMemory limit128 MB

Problem

A geometric sequence has first term aa and common ratio rr. Add its first nn terms and find the remainder of that total divided by mod\text{mod}.

That is, print a+ar+ar2++arn1a + ar + ar^2 + \cdots + ar^{n-1} modulo mod\text{mod}.

Input

The first line contains aa, rr, nn, and mod\text{mod}, separated by spaces. All four are natural numbers greater than or equal to 11 and less than or equal to 10910^9.

Output

On the first line, print the remainder of the sequence sum divided by mod\text{mod}.