#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 10, MOD = (int)1e9 + 7;
int n, p, tot, a[MAXN], sg[MAXN], f[MAXN][4], l, r, tl, tr, cnt[4];
void Renew() {
  tl = (a[l] + 1) * 3 - 1;
  tr = a[r] / 2 * 3 + (a[r] & 1);
}
int mex(int a, int b) {
  if (a && b) return 0;
  if (a != 1 && b != 1) return 1;
  return 2;
}
int main() {
  a[tot = 1] = 2;
  sg[tot] = 0;
  scanf("%d%d", &n, &p);
  l = 1, r = 1;
  for (; a[tot] < p;) {
    Renew();
    sg[++tot] = mex(sg[l], sg[r]);
    if (sg[tot] == sg[tot - 1]) --tot;
    a[tot] = min(tl, tr);
    if (tl == a[tot])
      ++l;
    else
      ++r;
  }
  a[tot] = min(a[tot], p);
  for (int i = 1; i <= tot; ++i) {
    int r = p - (a[i - 1] + 1), l = p - a[i];
    cnt[sg[i]] =
        ((long long)cnt[sg[i]] + (long long)(r + l) * (r - l + 1) / 2) % MOD;
  }
  f[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < 4; ++j)
      for (int k = 0; k < 3; ++k)
        f[i][j ^ k] =
            ((long long)f[i][j ^ k] + (long long)f[i - 1][j] * cnt[k]) % MOD;
  cout << ((long long)f[n][1] + f[n][2] + f[n][3]) % MOD << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
