#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007, inv2 = 500000004;
int n, p;
long long s[3], f[1010][4];
struct th {
  int l, r, c;
  th(int l, int r, int c) : l(l), r(r), c(c) {}
  th(int r) : r(r) {}
};
bool operator<(th x, th y) { return x.r < y.r; }
vector<th> t;
int mex(int x, int y) {
  static bool hh[3];
  hh[0] = hh[1] = hh[2] = 0;
  hh[x] = hh[y] = 1;
  for (int i = 0; i < 3; i++)
    if (!hh[i]) return i;
}
long long get(long long x, long long y) {
  return (x + y) % mod * (y - x + 1) % mod * inv2 % mod;
}
int main() {
  scanf("%d%d", &n, &p);
  if (p <= 2) {
    puts("0");
    return 0;
  }
  t.emplace_back(1, 2, 0);
  t.emplace_back(3, 3, 1);
  t.emplace_back(4, 4, 2);
  t.emplace_back(5, 6, 1);
  while (t.back().r < p) {
    int a1 = (t.back().r + 1) / 3;
    auto it1 = lower_bound(t.begin(), t.end(), a1);
    int a2 = t.back().r + 1 - a1;
    auto it2 = lower_bound(t.begin(), t.end(), a2);
    int b1 = it1->r, b2 = it2->r;
    int c = mex(it1->c, it2->c), r;
    if (b1 * 2 + 2 > b2) {
      b1 = b2 / 2;
      r = b1 + b2;
    } else
      r = b1 * 3 + 2;
    if (c == t.back().c)
      t.back().r = r;
    else
      t.emplace_back(t.back().r + 1, r, c);
  }
  for (const th &x : t) {
    if (x.l >= p) break;
    (s[x.c] += get(max(1, p - x.r), p - x.l)) %= mod;
  }
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 3; j++)
      for (int k = 0; k <= 3; k++)
        if ((j ^ k) <= 2) (f[i][j] += (f[i - 1][k] * s[j ^ k]) % mod) %= mod;
  }
  printf("%lld\n", (f[n][1] + f[n][2] + f[n][3]) % mod);
}
