#include <bits/stdc++.h>
using namespace std;
const long long NR = 1005;
const long long mod = 1e9 + 7;
long long n, p, sum, bs, bs2, ans;
long long tot = 0;
long long cnt = 500;
long long a[1005] = {
    0,         1,         3,         4,         5,         7,         9,
    13,        15,        19,        27,        39,        40,        57,
    58,        81,        85,        120,       121,       174,       179,
    255,       260,       363,       382,       537,       544,       780,
    805,       1146,      1169,      1632,      1718,      2415,      2447,
    3507,      3622,      5154,      5260,      7341,      7730,      10866,
    11011,     15780,     16298,     23190,     23669,     33033,     34784,
    48894,     49549,     71007,     73340,     104352,    106510,    148647,
    156527,    220020,    222970,    319530,    330029,    469581,    479294,
    668910,    704371,    990087,    1003364,   1437882,   1485130,   2113113,
    2156822,   3010092,   3169669,   4455390,   4515137,   6470466,   6683084,
    9509007,   9705698,   13545411,  14263510,  20049252,  20318116,  29117094,
    30073877,  42790530,  43675640,  60954348,  64185794,  90221631,  91431521,
    131026920, 135332446, 192557382, 196540379, 274294563, 288836072, 405997338,
    411441844, 589621137, 608996006, 866508216, 884431705, 1000000001};
long long b[1005] = {0, 0, 1, 2, 1, 2, 0, 1, 2, 0, 1, 2, 0, 2, 1, 2, 0, 2,
                     1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2,
                     0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2,
                     1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2,
                     0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2,
                     1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1};
long long dp[NR][NR];
long long getsum(long long l, long long r) {
  l = p + 1 - l, r = p + 1 - r;
  return ((l + r) * (l - r + 1) / 2) % mod;
}
long long jc[NR];
long long ksm(long long a, long long b) {
  a %= mod;
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
long long hf[NR], bhf[NR];
long long ny(long long a) { return ksm(a, mod - 2); }
long long C(long long a, long long b) {
  return jc[b] * ny(jc[a]) % mod * ny(jc[b - a]) % mod;
}
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
signed main() {
  n = read(), p = read();
  p -= 1;
  for (long long i = 1; i <= cnt; i++) {
    if (b[i] == 0) {
      if (a[i] > p) break;
      continue;
    }
    if (b[i] == 2) {
      if (a[i] > p) break;
      bs2 += getsum(a[i], min(a[i + 1] - 1, p));
      bs2 %= mod;
      continue;
    }
    if (a[i] > p) break;
    bs += getsum(a[i], min(a[i + 1] - 1, p));
    bs %= mod;
  }
  long long all = getsum(1, p);
  dp[0][0] = 1;
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= 3; j++) {
      dp[i + 1][j] += dp[i][j] * (all - bs - bs2);
      dp[i + 1][j] % mod;
      dp[i + 1][j] += dp[i][j ^ 1] * bs;
      dp[i + 1][j] %= mod;
      dp[i + 1][j] += dp[i][j ^ 2] * bs2;
      dp[i + 1][j] %= mod;
    }
  long long ans = 0;
  for (long long i = 1; i <= 3; i++) ans += dp[n][i];
  printf("%lld\n", (ans % mod + mod) % mod);
  return 0;
}
