#include <bits/stdc++.h>
using namespace std;
long long P;
long long C[1100][1100], q[1000000], p1[1100], p2[1100], p0[1100];
int val[1000000];
int qb;
long long Calc(long long u, long long v) {
  long long n = v - u + 1, d = (2 * P - u - v);
  return (n * d / 2) % 1000000007;
}
int Grundy(long long u) {
  long long v = u / 3;
  int vst[3], k;
  vst[0] = vst[1] = vst[2] = 0;
  k = upper_bound(q, q + qb, v) - q;
  k--;
  vst[val[k]] = 1;
  v = u - u / 3;
  k = upper_bound(q, q + qb, v) - q;
  k--;
  vst[val[k]] = 1;
  for (k = 0; k <= 2; k++)
    if (!vst[k]) return k;
}
int main() {
  int N, p, k, i, j, ok;
  long long res, tot, n0, n1, n2, u, v;
  for (i = 0; i < 1100; i++) C[i][i] = C[i][0] = 1;
  for (i = 2; i < 1100; i++)
    for (j = 1; j < i; j++)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % 1000000007;
  scanf("%d%I64d", &N, &P);
  set<long long> S;
  S.clear();
  if (4 <= P + 1) S.insert(4);
  if (9 <= P + 1) S.insert(9);
  qb = 0;
  q[qb++] = 1;
  q[qb++] = 3;
  val[0] = ok = 0;
  while (!S.empty()) {
    u = *S.begin();
    S.erase(S.begin());
    if (u <= q[qb - 1]) continue;
    k = Grundy(u - 1);
    if (k == val[qb - 2]) {
      q[qb - 1] = u;
    } else {
      q[qb++] = u;
      val[qb - 2] = k;
      if ((u * 3 - 1) / 2 <= P + 1)
        S.insert((u * 3 - 1) / 2);
      else if (!ok)
        S.insert(P + 1), ok = 1;
      if (u * 3 <= P + 1)
        S.insert(u * 3);
      else if (!ok)
        S.insert(P + 1), ok = 1;
      u = q[qb - 2];
      if ((u * 3 - 1) / 2 <= P + 1)
        S.insert((u * 3 - 1) / 2);
      else if (!ok)
        S.insert(P + 1), ok = 1;
      if (u * 3 <= P + 1)
        S.insert(u * 3);
      else if (!ok)
        S.insert(P + 1), ok = 1;
    }
  }
  n0 = n1 = n2 = 0;
  for (i = 0; i < qb - 1; i++) {
    u = q[i];
    v = q[i + 1] - 1;
    v = min(v, P);
    if (val[i] == 0) {
      n0 += Calc(u, v);
      n0 %= 1000000007;
    } else if (val[i] == 1) {
      n1 += Calc(u, v);
      n1 %= 1000000007;
    } else if (val[i] == 2) {
      n2 += Calc(u, v);
      n2 %= 1000000007;
    }
  }
  tot = Calc(1, P - 1);
  res = 1;
  for (i = 0; i < N; i++) res = res * tot % 1000000007;
  p1[0] = p2[0] = p0[0] = 1;
  for (i = 1; i <= N; i++) {
    p0[i] = p0[i - 1] * n0 % 1000000007;
    p1[i] = p1[i - 1] * n1 % 1000000007;
    p2[i] = p2[i - 1] * n2 % 1000000007;
  }
  for (i = 0; i <= N; i += 2) {
    for (j = 0; j + i <= N; j += 2) {
      res -= C[N][i] * C[N - i][j] % 1000000007 * p1[i] % 1000000007 * p2[j] %
             1000000007 * p0[N - i - j] % 1000000007;
      res = (res % 1000000007 + 1000000007) % 1000000007;
    }
  }
  printf("%I64d\n", res);
  return 0;
}
