Under construction

Still being built. Things may change

Rock-Paper-Scissors

Time limit2sMemory limit512 MB

Summary
Given n rounds of random rock-paper-scissors and p prime, compute the expected GCD score of the win counts times 3^(2n), modulo p.
Level

Medium7 of 10

Topics
Math, Number theory, Combinatorics, Probability
Solved
No attempts yet

Statement

Alice and Bob are going to play the famous game "Rock-Paper-Scissors". Both of them dislike thinking much, so both use a random strategy: each picks rock, paper, or scissors with equal probability.

They play the game nn times and then compute a score ss as follows. If Alice won aa times, Bob won bb times, and the remaining n−a−bn - a - b games were draws, the score is the greatest common divisor of aa and bb. If aa or bb is 00, the greatest common divisor of aa and bb is defined as a+ba + b.

Calculate the expected value of s⋅32ns \cdot 3^{2 n}. The answer is always an integer. Because it may be very large, print its remainder modulo pp.

Input

The input contains two integers nn and pp (1≤n≤1051 \le n \le 10^5, 108≤p≤10910^8 \le p \le 10^9, pp is prime).

Output

Print a single integer: the answer modulo pp.

Examples2

  1. Example 1

    Input
    1 998244353
    
    Expected output
    6
    
  2. Example 2

    Input
    2 998244353
    
    Expected output
    90