#include <bits/stdc++.h>
using namespace std;
int sg[100010], v[3];
inline void Try() {
  sg[0] = sg[1] = sg[2] = 0;
  for (int i = 3; i <= 100000; i++) {
    v[0] = v[1] = v[2] = 0;
    v[sg[i / 3]] = v[sg[i - (i / 3)]] = 1;
    for (int j = 0; j < 3; j++)
      if (!v[j]) {
        sg[i] = j;
        break;
      }
  }
  for (int j, i = 1; i <= 100000; i = j) {
    j = i;
    while (sg[j] == sg[i] && j <= 100000) j++;
    printf("%d %d %d\n", sg[i], i, j - 1);
  }
}
int t, t2, t0, t1, nm[3];
pair<int, int> A1[1010], A0[1010], A2[1010], A[1010];
inline int Find0(int x) {
  int l = 1, r = t0, ps = 0;
  while (l <= r) {
    int md = (l + r) / 2;
    if (A0[md].first <= x && x <= A0[md].second) {
      ps = md;
      break;
    }
    if (A0[md].second < x)
      l = md + 1;
    else
      r = md - 1;
  }
  if (!ps)
    return (x);
  else
    return (A0[ps].second + 1);
}
inline int Sum(int n) { return (n * 1ll * (n + 1) / 2 % 1000000007); }
inline int Cal(pair<int, int>* A, int t, int n) {
  int sum = 0;
  for (int i = 1; i <= t; i++) {
    int l = A[i].first, r = A[i].second;
    sum = (sum + (r - l + 1) * 1ll * n % 1000000007) % 1000000007;
    sum = (sum - (Sum(r) - Sum(l - 1) + 1000000007) % 1000000007 + 1000000007) %
          1000000007;
  }
  return (sum);
}
inline void Get(int lm) {
  A0[t0 = 1] = make_pair(1, 2), A0[t0 = 2] = make_pair(9, 12);
  while (1) {
    int l = A0[t0].second;
    if (l % 2 == 0)
      l = l / 2 * 3 + 1;
    else
      l = (l + 1) / 2 * 3 - 1;
    int r = min(A0[t0].first * 3 - 1, lm);
    if (l > lm) break;
    A0[++t0] = make_pair(l, r);
  }
  A1[t1 = 1] = make_pair(3, 3), A1[t1 = 2] = make_pair(5, 6);
  while (1) {
    int l = A1[t1].second;
    if (l % 2 == 0)
      l = l / 2 * 3 + 1;
    else
      l = (l + 1) / 2 * 3 - 1;
    int r = min(A1[t1].first * 3 - 1, lm);
    if (l > lm)
      break;
    else
      l = Find0(l);
    A1[++t1] = make_pair(l, r);
  }
  for (int i = 1; i <= t0; i++) {
    if (A0[i].first > lm) {
      t0 = i - 1;
      break;
    }
    A0[i].second = min(A0[i].second, lm), A[++t] = A0[i];
  }
  for (int i = 1; i <= t1; i++) {
    if (A1[i].first > lm) {
      t1 = i - 1;
      break;
    }
    A1[i].second = min(A1[i].second, lm), A[++t] = A1[i];
  }
  sort(A + 1, A + 1 + t);
  for (int i = 1; i < t; i++)
    if (A[i].second + 1 < A[i + 1].first)
      A2[++t2] = make_pair(A[i].second + 1, A[i + 1].first - 1);
  if (A[t].second < lm) A2[++t2] = make_pair(A[t].second + 1, lm);
  nm[0] = Cal(A0, t0, lm);
  nm[1] = Cal(A1, t1, lm);
  nm[2] = Cal(A2, t2, lm);
}
int ans, f[2010][4], n, p;
inline void Add(int& a, int b) { a = (a + b) % 1000000007; }
int main() {
  scanf("%d%d", &n, &p);
  Get(p);
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k < 4; k++)
      for (int j = 0; j < 3; j++)
        Add(f[i][k ^ j], f[i - 1][k] * 1ll * nm[j] % 1000000007);
  }
  Add(ans, f[n][1]);
  Add(ans, f[n][2]);
  Add(ans, f[n][3]);
  printf("%d\n", ans);
}
