PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CT0000056. Dãy bậc thang (Staircase Sequence)

Bài tập này chưa được xếp vào lộ trình.

Dãy bậc thang (Staircase Sequence)

Staircase Sequence

Version: Phuoc Hung OJ Extended

Problem Statement

A positive integer sequence A1,A2,…A_1,A_2,\ldots is defined by

A1=2,Ai=Ai−1+2i(i≥2).A_1=2,\qquad A_i=A_{i-1}+2i\quad (i\ge2).

For a positive integer NN, define

SN=∑i=1NAi.S_N=\sum_{i=1}^{N}A_i.

Since NN can be very large, SNS_N may exceed ordinary integer types. Compute the remainder of SNS_N when divided by the positive integer MM.

Input

The first line contains the positive integer NN.

The second line contains the positive integer MM.

Output

Print one integer: SN mod MS_N \bmod M.

Subtasks

  • Subtask 1 (30%): 1≤N≤1061 \le N \le 10^6, 1≤M≤1091 \le M \le 10^9.
  • Subtask 2 (30%): 1≤N≤10121 \le N \le 10^{12}, 1≤M≤1091 \le M \le 10^9.
  • Subtask 3 (40%): 1≤N≤10181 \le N \le 10^{18}, 1≤M≤1091 \le M \le 10^9.

Examples

Example 1

Input

3
100

Output

20

Explanation

The first three terms are 2,6,122,6,12, so S3=2+6+12=20S_3=2+6+12=20.

Example 2

Input

10
97

Output

52

Explanation

The sum of the first ten terms is 440440. Since 440=4⋅97+52440=4\cdot97+52, the remainder is 5252.

Example 3

Input

5
7

Output

0

Explanation

The first five terms are 2,6,12,20,302,6,12,20,30; their sum is 7070, which is divisible by 77.