#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
const int mod = 1e9 + 7;
int n, p, m, s[N], t[N], v[N];
int cnt[3], dp[1010][4];
void inc(int& a, int b) {
  if (a += b, a >= mod) a -= mod;
}
int sum(int l, int r) { return 1LL * (l + r) * (r - l + 1) / 2 % mod; }
int mex(int x, int y) {
  if (x && y) return 0;
  return x + y == 1 ? 2 : 1;
}
void prepare() {
  m = 1;
  s[1] = 1, t[1] = 2;
  v[1] = 0;
  for (int l = 1, r = 1; t[m] < p;) {
    int x = t[m] + 1;
    while (x / 3 > t[l]) ++l;
    while (x - x / 3 > t[r]) ++r;
    s[++m] = x;
    v[m] = mex(v[l], v[r]);
    int d = min(t[l] - x / 3, t[r] - (x - x / 3));
    t[m] = s[m] + d;
    if (v[m] == v[m - 1]) t[m - 1] = t[m], --m;
  }
  t[m] = min(t[m], p);
  for (int i = 1; i <= m; ++i) inc(cnt[v[i]], sum(p - t[i], p - s[i]));
}
int main() {
  scanf("%d %d", &n, &p);
  prepare();
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int x = 0; x <= 3; ++x)
      for (int y = 0; y <= 2; ++y)
        inc(dp[i][x ^ y], 1LL * dp[i - 1][x] * cnt[y] % mod);
  int ans = 0;
  for (int x = 1; x <= 3; ++x) inc(ans, dp[n][x]);
  printf("%d\n", ans);
}
