#include <bits/stdc++.h>
using namespace std;
int L[1111], R[1111], val[1111], cnt[4];
long long dp[1111][4];
int n, p;
void calc_grundy() {
  L[0] = 1, R[0] = 3, val[0] = 0;
  int nn = 1;
  while (1) {
    if (R[nn - 1] > p) {
      R[nn - 1] = p + 1;
      break;
    }
    L[nn] = R[nn - 1];
    int pre[2] = {L[nn] / 3, L[nn] - L[nn] / 3}, pos[2] = {0, 0}, v[2];
    int mn = 1000000000;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; pre[i] >= R[j] && j < nn;) pos[i] = ++j;
      mn = min(mn, R[pos[i]] - pre[i]);
      v[i] = val[pos[i]];
    }
    R[nn] = L[nn] + mn;
    if (v[0] > v[1]) swap(v[0], v[1]);
    if (v[0])
      val[nn] = 0;
    else {
      if (v[1])
        val[nn] = 3 - v[1];
      else
        val[nn] = 1;
    }
    if (val[nn] == val[nn - 1])
      R[nn - 1] = R[nn];
    else
      nn++;
  }
  for (int i = 0; i < nn; i++) {
    cnt[val[i]] = (cnt[val[i]] + (long long)(2 * p - L[i] - R[i] + 1) *
                                     (R[i] - L[i]) / 2 % 1000000007) %
                  1000000007;
  }
}
int main() {
  cin >> n >> p;
  calc_grundy();
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j] =
            (dp[i][j] + dp[i - 1][k] * cnt[k ^ j] % 1000000007) % 1000000007;
      }
    }
  }
  cout << (dp[n][1] + dp[n][2] + dp[n][3]) % 1000000007 << endl;
  return 0;
}
