#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int MOD = 1000000007;
int N, P;
int dp[MAXN][4];
int a[MAXN], b[MAXN];
int cnt[3];
void load() { scanf("%d%d", &N, &P); }
inline int add(int x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
  else if (x < 0)
    x += MOD;
  return x;
}
inline int mul(int x, int y) { return (long long)x * y % MOD; }
void add(int lo, int hi, int val) {
  cnt[val] =
      add(cnt[val], mul(hi - lo + 1, add(P, -mul(add(lo, hi), (MOD + 1) / 2))));
}
int solve() {
  if (P == 1) return 0;
  a[0] = 1;
  b[0] = 0;
  a[1] = 3;
  b[1] = 1;
  a[2] = 4;
  b[2] = 2;
  a[3] = 5;
  b[3] = 1;
  a[4] = 7;
  b[4] = 2;
  a[5] = 9;
  b[5] = 0;
  a[6] = 13;
  b[6] = 1;
  a[7] = 15;
  b[7] = 2;
  a[8] = 19;
  b[8] = 0;
  a[9] = 27;
  b[9] = 1;
  a[10] = 39;
  b[10] = 2;
  a[11] = 40;
  b[11] = 0;
  a[12] = 57;
  b[12] = 2;
  a[13] = 58;
  b[13] = 1;
  a[14] = 81;
  b[14] = 2;
  a[15] = 85;
  b[15] = 0;
  a[16] = 120;
  b[16] = 2;
  a[17] = 121;
  b[17] = 1;
  a[18] = 174;
  b[18] = 2;
  a[19] = 179;
  b[19] = 0;
  a[20] = 255;
  b[20] = 2;
  a[21] = 260;
  b[21] = 1;
  a[22] = 363;
  b[22] = 2;
  a[23] = 382;
  b[23] = 0;
  a[24] = 537;
  b[24] = 2;
  a[25] = 544;
  b[25] = 1;
  a[26] = 780;
  b[26] = 2;
  a[27] = 805;
  b[27] = 0;
  a[28] = 1146;
  b[28] = 2;
  a[29] = 1169;
  b[29] = 1;
  a[30] = 1632;
  b[30] = 2;
  a[31] = 1718;
  b[31] = 0;
  a[32] = 2415;
  b[32] = 2;
  a[33] = 2447;
  b[33] = 1;
  a[34] = 3507;
  b[34] = 2;
  a[35] = 3622;
  b[35] = 0;
  a[36] = 5154;
  b[36] = 2;
  a[37] = 5260;
  b[37] = 1;
  a[38] = 7341;
  b[38] = 2;
  a[39] = 7730;
  b[39] = 0;
  a[40] = 10866;
  b[40] = 2;
  a[41] = 11011;
  b[41] = 1;
  a[42] = 15780;
  b[42] = 2;
  a[43] = 16298;
  b[43] = 0;
  a[44] = 23190;
  b[44] = 2;
  a[45] = 23669;
  b[45] = 1;
  a[46] = 33033;
  b[46] = 2;
  a[47] = 34784;
  b[47] = 0;
  a[48] = 48894;
  b[48] = 2;
  a[49] = 49549;
  b[49] = 1;
  a[50] = 71007;
  b[50] = 2;
  a[51] = 73340;
  b[51] = 0;
  a[52] = 104352;
  b[52] = 2;
  a[53] = 106510;
  b[53] = 1;
  a[54] = 148647;
  b[54] = 2;
  a[55] = 156527;
  b[55] = 0;
  a[56] = 220020;
  b[56] = 2;
  a[57] = 222970;
  b[57] = 1;
  a[58] = 319530;
  b[58] = 2;
  a[59] = 330029;
  b[59] = 0;
  a[60] = 469581;
  b[60] = 2;
  a[61] = 479294;
  b[61] = 1;
  a[62] = 668910;
  b[62] = 2;
  a[63] = 704371;
  b[63] = 0;
  a[64] = 990087;
  b[64] = 2;
  a[65] = 1003364;
  b[65] = 1;
  a[66] = 1437882;
  b[66] = 2;
  a[67] = 1485130;
  b[67] = 0;
  a[68] = 2113113;
  b[68] = 2;
  a[69] = 2156822;
  b[69] = 1;
  a[70] = 3010092;
  b[70] = 2;
  a[71] = 3169669;
  b[71] = 0;
  a[72] = 4455390;
  b[72] = 2;
  a[73] = 4515137;
  b[73] = 1;
  a[74] = 6470466;
  b[74] = 2;
  a[75] = 6683084;
  b[75] = 0;
  a[76] = 9509007;
  b[76] = 2;
  a[77] = 9705698;
  b[77] = 1;
  a[78] = 13545411;
  b[78] = 2;
  a[79] = 14263510;
  b[79] = 0;
  a[80] = 20049252;
  b[80] = 2;
  a[81] = 20318116;
  b[81] = 1;
  a[82] = 29117094;
  b[82] = 2;
  a[83] = 30073877;
  b[83] = 0;
  a[84] = 42790530;
  b[84] = 2;
  a[85] = 43675640;
  b[85] = 1;
  a[86] = 60954348;
  b[86] = 2;
  a[87] = 64185794;
  b[87] = 0;
  a[88] = 90221631;
  b[88] = 2;
  a[89] = 91431521;
  b[89] = 1;
  a[90] = 131026920;
  b[90] = 2;
  a[91] = 135332446;
  b[91] = 0;
  a[92] = 192557382;
  b[92] = 2;
  a[93] = 196540379;
  b[93] = 1;
  a[94] = 274294563;
  b[94] = 2;
  a[95] = 288836072;
  b[95] = 0;
  a[96] = 405997338;
  b[96] = 2;
  a[97] = 411441844;
  b[97] = 1;
  a[98] = 589621137;
  b[98] = 2;
  a[99] = 608996006;
  b[99] = 0;
  a[100] = 866508216;
  b[100] = 2;
  a[101] = 884431705;
  b[101] = 1;
  a[102] = 1000000000;
  b[102] = 0;
  for (int i = 0; i < MAXN; i++) {
    if (a[i + 1] >= P) {
      add(a[i], P - 1, b[i]);
      break;
    }
    add(a[i], a[i + 1] - 1, b[i]);
  }
  dp[0][0] = 1;
  for (int i = 1; i <= N; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 3; k++)
        dp[i][j] = add(dp[i][j], mul(cnt[k], dp[i - 1][j ^ k]));
  int sol = 0;
  for (int i = 1; i < 4; i++) sol = add(sol, dp[N][i]);
  return sol;
}
int main() {
  load();
  printf("%d\n", solve());
  return 0;
}
