#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5, P = 1e9 + 7;
int n, p, cnt[3], dp[N][4] = {1};
struct Info {
  int l, r, t;
  Info(int a = 0, int b = 0, int c = 0) { l = a, r = b, t = c; }
};
vector<Info> vec;
int _mul(int a, int b) { return 1LL * a * b % P; }
int _sum(int a, int b) {
  if ((a += b) < 0) a += P;
  return a < P ? a : a - P;
}
int power(int a, int b) {
  int ans = 1;
  for (; b; a = _mul(a, a), b >>= 1) ans = b & 1 ? _mul(ans, a) : ans;
  return ans;
}
int main() {
  cin >> n >> p;
  vec.push_back({1, min(3, p + 1), 0});
  for (int l = 3, p1 = 0, p2 = 0; l < p;) {
    while (vec[p1].r <= l / 3) p1++;
    while (vec[p2].r <= l - l / 3) p2++;
    int x = vec[p1].t && vec[p2].t ? 0 : vec[p1].t + vec[p2].t & 1 ? 2 : 1;
    int r = min({3LL * vec[p1].r, 3LL * vec[p2].r - 1 >> 1, 1LL * p});
    if (vec.back().t ^ x) vec.push_back({l, r, x});
    l = vec.back().r = r;
  }
  for (auto x : vec)
    cnt[x.t] =
        _sum(cnt[x.t], _sum(_mul(p + 1, x.r - x.l),
                            (x.l * (x.l + 1LL) - x.r * (x.r + 1LL) >> 1) % P));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 3; k++)
        dp[i + 1][j ^ k] = _sum(dp[i + 1][j ^ k], _mul(dp[i][j], cnt[k]));
  cout << _sum(_sum(dp[n][1], dp[n][2]), dp[n][3]);
}
