#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
vector<long long> l, r, sg;
long long Szukaj(long long x) {
  for (int i = 0; i < l.size(); ++i)
    if (x >= l[i] && x <= r[i]) return i;
}
long long Mex(long long x, long long y) {
  if (x != 0 && y != 0) return 0;
  if (x != 1 && y != 1) return 1;
  return 2;
}
int main() {
  ios_base::sync_with_stdio(0);
  long long n, p;
  cin >> n >> p;
  if (p <= 2) {
    cout << "0";
    return 0;
  }
  l.push_back(0);
  r.push_back(2);
  sg.push_back(0);
  l.push_back(3);
  r.push_back(3);
  sg.push_back(1);
  while (r.back() < p) {
    long long akt = r.back() + 1;
    long long d1 = akt / 3;
    long long gdzie1 = Szukaj(d1);
    long long ilePrawo1 = r[gdzie1] - d1 + 1;
    long long x;
    if (akt % 3 == 0) x = 3 * ilePrawo1;
    if (akt % 3 == 1) x = 3 * ilePrawo1 - 1;
    if (akt % 3 == 2) x = 3 * ilePrawo1 - 2;
    long long d2 = akt - akt / 3;
    long long gdzie2 = Szukaj(d2);
    long long ilePrawo2 = r[gdzie2] - d2 + 1;
    long long y;
    if (akt % 3 == 1)
      y = (ilePrawo2 % 2 == 0) ? 3 * ilePrawo2 / 2
                               : 3 * (ilePrawo2 - 1) / 2 + 1;
    if (akt % 3 == 2)
      y = (ilePrawo2 % 2 == 0) ? 3 * ilePrawo2 / 2
                               : 3 * (ilePrawo2 - 1) / 2 + 2;
    if (akt % 3 == 0)
      y = (ilePrawo2 % 2 == 0) ? 3 * (ilePrawo2 - 2) / 2 + 2
                               : 3 * (ilePrawo2 - 1) / 2 + 1;
    long long mex = Mex(sg[gdzie1], sg[gdzie2]);
    if (mex == sg.back())
      r[r.size() - 1] = akt + min(x, y) - 1;
    else {
      l.push_back(akt);
      r.push_back(akt + min(x, y) - 1);
      sg.push_back(mex);
    }
  }
  r[r.size() - 1] = min(r.back(), p);
  ++l[0];
  vector<long long> il(3, 0);
  for (int i = 0; i < l.size(); ++i) {
    long long w = p * (r[i] - l[i] + 1) % MOD;
    long long q = (l[i] + r[i]) * (r[i] - l[i] + 1) / 2 % MOD;
    w = w - q;
    if (w < 0) w += MOD;
    il[sg[i]] = (il[sg[i]] + w) % MOD;
  }
  vector<vector<long long> > dp(n, vector<long long>(4, 0));
  for (int i = 0; i < 3; ++i) dp[0][i] = il[i] % MOD;
  for (int i = 0; i < n - 1; ++i)
    for (int j = 0; j < 4; ++j)
      for (int k = 0; k < 3; ++k)
        dp[i + 1][j ^ k] = (dp[i + 1][j ^ k] + dp[i][j] * il[k]) % MOD;
  cout << (dp[n - 1][1] + dp[n - 1][2] + dp[n - 1][3]) % MOD;
  return 0;
}
