using System; using System.Linq; using System.Collections.Generic; using Debug = System.Diagnostics.Debug; using SB = System.Text.StringBuilder; //using System.Numerics; //using static System.Math; using System.IO; using Number = ModInt; namespace Program { public class Solver { public void Solve() { long n = rl; ModInt.Mod = ri; //naive(n, ModInt.Mod + 1); //for (int _ = 1; _ < 1000; _++) { //n = _; //naive(2, n, ModInt.Mod); var mat = new Matrix(2, 2); mat[0, 0] = 4; mat[0, 1] = -1; mat[1, 0] = 1; ModInt.Mod -= 2; long k = ModInt.Pow(2, n).num; ModInt.Mod += 2; var res = Matrix.Pow(mat, k); var ans = (res[1, 0] * 4 + res[1, 1] * 2) - 2; Console.WriteLine(ans); } } void naive(long v, long n, int mod) { Debug.WriteLine($"{v} {n} {mod}"); var vis = new int[mod]; vis[v] = 0; for (int i = 0; i < n; i++) { v = (v * v + v * 4) % mod; if (true && vis[v] == int.MaxValue) { vis[v] = i + 1; } else { var T = (i + 1) - vis[v]; var rem = (n - vis[v]) % T; naive(v, rem, mod); return; } } Console.WriteLine(v); } const long INF = 1L << 60; int[] dx = { 1, 0, -1, 0 }; int[] dy = { 0, 1, 0, -1 }; //* int ri { get { return sc.Integer(); } } long rl { get { return sc.Long(); } } double rd { get { return sc.Double(); } } string rs { get { return sc.Scan(); } } //*/ public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput()); static T[] Enumerate(int n, Func f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; } static public void Swap(ref T a, ref T b) { var tmp = a; a = b; b = tmp; } } } #region main static class Ex { static public string AsString(this IEnumerable ie) { return new string(ie.ToArray()); } static public string AsJoinedString(this IEnumerable ie, string st = " ") { return string.Join(st, ie.Select(x => x.ToString()).ToArray()); //return string.Join(st, ie); } static public void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var solver = new Program.Solver(); solver.Solve(); Console.Out.Flush(); } } #endregion #region Ex namespace Program.IO { using System.IO; using System.Text; using System.Globalization; public class Printer: StreamWriter { public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } } public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } public readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public string ScanLine() { var sb = new StringBuilder(); for (var b = Char(); b != '\n' && b != 0; b = (char)read()) if (b != '\r') sb.Append(b); return sb.ToString(); } public long Long() { return isEof ? long.MinValue : long.Parse(Scan()); } public int Integer() { return isEof ? int.MinValue : int.Parse(Scan()); } public double Double() { return isEof ? double.NaN : double.Parse(Scan(), CultureInfo.InvariantCulture); } } } #endregion #region Matrix public class Matrix { int row, col; public Number[] mat; public Number this[int r, int c] { get { return mat[r * col + c]; } set { mat[r * col + c] = value; } } public Matrix(int r, int c) { row = r; col = c; mat = new Number[row * col]; } public static Matrix operator +(Matrix l, Matrix r) { check(l, r); var ret = new Matrix(l.row, l.col); for (int i = 0; i < l.row; i++) for (int j = 0; j < l.col; j++) ret.mat[i * ret.col + j] = l.mat[i * l.col + j] + r.mat[i * r.col + j]; return ret; } public static Matrix operator *(Matrix l, Matrix r) { checkMul(l, r); var ret = new Matrix(l.row, r.col); for (int i = 0; i < l.row; i++) for (int k = 0; k < l.col; k++) for (int j = 0; j < r.col; j++) ret.mat[i * r.col + j] = (ret.mat[i * r.col + j] + l.mat[i * l.col + k] * r.mat[k * r.col + j]); return ret; } public static Matrix Pow(Matrix m, long n) { var ret = new Matrix(m.row, m.col); for (int i = 0; i < m.row; i++) ret.mat[i * m.col + i] = 1; for (; n > 0; m *= m, n >>= 1) if ((n & 1) == 1) ret = ret * m; return ret; } public static Matrix Trans(Matrix m) { var ret = new Matrix(m.col, m.row); for (int i = 0; i < m.row; i++) for (int j = 0; j < m.col; j++) ret.mat[j * m.col + i] = m.mat[i * m.col + j]; return ret; } [System.Diagnostics.Conditional("DEBUG")] static private void check(Matrix a, Matrix b) { if (a.row != b.row || a.col != b.col) throw new Exception("row and col have to be same."); } [System.Diagnostics.Conditional("DEBUG")] static private void checkMul(Matrix a, Matrix b) { if (a.col != b.row) throw new Exception("row and col have to be same."); } public Number[][] Items { get { var a = new Number[row][]; for (int i = 0; i < row; i++) { a[i] = new Number[col]; for (int j = 0; j < col; j++) a[i][j] = mat[i * col + j]; } return a; } } public override string ToString() { return string.Format("{0}*{1}", row, col); } } #endregion //剰余環 #region ModInt public struct ModInt { public static int Mod = (int)1e9 + 7; public int num; public ModInt(int n) { num = n; } public override string ToString() { return num.ToString(); } public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; } public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; } public static ModInt operator *(ModInt l, ModInt r) { return new ModInt((int)(l.num * (long)r.num % Mod)); } public static implicit operator ModInt(int n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); } public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt((int)n); } public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); } public static ModInt Pow(int v, long k) { int ret = 1; for (k %= Mod - 1; k > 0; k >>= 1, v = (int)(v * (long)v % Mod)) if ((k & 1) == 1) ret = (int)(ret * (long)v % Mod); return new ModInt(ret); } public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); } } #endregion