#include <bits/stdc++.h>
const int N = 10000, MOD = 1000000007;
int n, p, cnt[10], f[N][10], m;
std::pair<int, int> sg[1000];
inline int SG(int x) {
  int l = 1, r = m + 1;
  while (l + 1 < r) {
    const int mid = (l + r) / 2;
    if (sg[mid].first <= x)
      l = mid;
    else
      r = mid;
  }
  return l;
}
int main() {
  scanf("%d%d", &n, &p);
  if (p <= 3) return puts("0");
  sg[++m] = std::make_pair(1, 0);
  sg[++m] = std::make_pair(3, 1);
  int x = 1, y = 1;
  while (1) {
    int l = 2, r = p;
    while (l + 1 < r) {
      const int mid = (l + r) / 2;
      if (mid / 3 >= sg[x + 1].first)
        r = mid;
      else
        l = mid;
    }
    int i1 = r;
    if (y < m) {
      l = 2;
      r = p;
      while (l + 1 < r) {
        const int mid = (l + r) / 2;
        if (mid - mid / 3 >= sg[y + 1].first)
          r = mid;
        else
          l = mid;
      }
    }
    int i2 = r, i = std::min(i1, i2), v;
    if (i >= p) break;
    for (int j = 0; j < 3; ++j)
      if (sg[x = SG(i / 3)].second != j && sg[y = SG(i - i / 3)].second != j) {
        v = j;
        break;
      }
    sg[++m] = std::make_pair(i, v);
    if (v == sg[m - 1].second) --m;
  }
  if (sg[m].first < p) {
    ++m;
    sg[m].first = p + 1;
  }
  for (int i = 2; i <= m; ++i) {
    int r = p - sg[i - 1].first, l = p - (sg[i].first - 1);
    cnt[sg[i - 1].second] =
        (cnt[sg[i - 1].second] + 1ll * (r - l + 1) * (l + r) / 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] = (f[i][j] + 1ll * f[i - 1][j ^ k] * cnt[k]) % MOD;
  printf("%I64d\n", ((long long)f[n][1] + f[n][2] + f[n][3]) % MOD);
}
