#include <bits/stdc++.h>
using namespace std;
const int mode = 1e9 + 7;
int n, m, p, s[1001000], t[1001000], v[1001000], cnt[3];
int dp[1010][5];
int calc(int l, int r) { return (long long)(l + r) * (r - l + 1) / 2 % mode; }
int mex(int x, int y) {
  if (x && y) return 0;
  return x + y == 1 ? 2 : 1;
}
void prep() {
  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] = x + 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++)
    cnt[v[i]] = cnt[v[i]] + calc(p - t[i], p - s[i]) < mode
                    ? cnt[v[i]] + calc(p - t[i], p - s[i])
                    : cnt[v[i]] + calc(p - t[i], p - s[i]) - mode;
}
int main() {
  cin >> n >> p;
  prep();
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int x = 0; x <= 3; x++)
      for (int j = 0; j <= 2; j++)
        dp[i][x ^ j] =
            dp[i][x ^ j] + (long long)dp[i - 1][x] * cnt[j] % mode < mode
                ? dp[i][x ^ j] + (long long)dp[i - 1][x] * cnt[j] % mode
                : dp[i][x ^ j] + (long long)dp[i - 1][x] * cnt[j] % mode - mode;
  int ans = 0;
  for (int i = 1; i <= 3; i++)
    ans = ans + dp[n][i] < mode ? ans + dp[n][i] : ans + dp[n][i] - mode;
  cout << ans;
}
