#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
class SG {
 public:
  int l, r, sg;
  SG() {}
  SG(int _l, int _r, int _sg) {
    l = _l;
    r = _r;
    sg = _sg;
  }
} a[1005];
int getsg(int x, int y) {
  if (x != 0 && y != 0) return 0;
  if (x != 1 && y != 1) return 1;
  return 2;
}
const int MOD = 1e9 + 7;
long long trapezoid(int top, int bot) {
  return (top + bot) * 1LL * (top - bot + 1) / 2;
}
void add(long long &x, long long v) { x = (x + v) % MOD; }
int main(void) {
  int n, p;
  scanf("%d%d", &n, &p);
  int m = 1, last = 2;
  a[0] = SG(0, 2, 0);
  while (last < p) {
    int x, u, v, i, j;
    x = last + 1;
    u = x / 3;
    v = x - x / 3;
    for (i = 0; i < m; i++)
      if (u <= a[i].r && u >= a[i].l) break;
    for (j = 0; j < m; j++)
      if (v <= a[j].r && v >= a[j].l) break;
    int ll = x, rr = p, mm, xx = x;
    while (ll <= rr) {
      mm = (ll + rr) / 2;
      if (mm / 3 > a[i].r || mm - mm / 3 > a[j].r)
        rr = mm - 1;
      else {
        xx = mm;
        ll = mm + 1;
      }
    }
    a[m++] = SG(last + 1, xx, getsg(a[i].sg, a[j].sg));
    if (m > 1 && a[m - 1].sg == a[m - 2].sg) {
      --m;
      a[m - 1].r = a[m].r;
    }
    last = xx;
  }
  a[0].l = 1;
  long long c[4] = {};
  for (int i = 0; i < m; i++) {
    add(c[a[i].sg], trapezoid(p - a[i].l, p - a[i].r));
  }
  long long ans[1005][4] = {};
  ans[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 3; k++)
        add(ans[i + 1][j ^ k], ans[i][j] * c[k] % MOD);
  }
  long long sol = 0;
  sol = ans[n][1] + ans[n][2] + ans[n][3];
  sol = (sol % MOD + MOD) % MOD;
  printf("%I64d\n", sol);
  return 0;
}
