#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007, inv2 = 500000004;
long long n, p;
long long s[4];
struct th {
  long long l, r;
  int c;
  th(long long l, long long r, int c) : l(l), r(r), c(c) {}
  th(long long r) : r(r) {}
};
bool operator<(th x, th y) { return x.r < y.r; }
vector<th> t;
struct matrix {
  long long s[4][4];
  int x, y;
  matrix() { memset(s, 0, sizeof(s)); }
  long long *operator[](int x) { return s[x]; }
} b, ans;
matrix operator*(matrix x, matrix y) {
  matrix ans;
  ans.x = x.x;
  ans.y = y.y;
  for (int i = 0; i < x.x; i++)
    for (int j = 0; j < y.y; j++)
      for (int k = 0; k < y.x; k++)
        (ans[i][j] += x[i][k] * y[k][j] % mod) %= mod;
  return ans;
}
matrix Pow(matrix x, long long y) {
  if (y == 1) return x;
  matrix ans = Pow(x, y >> 1);
  ans = ans * ans;
  if (y & 1) ans = ans * x;
  return ans;
}
int mex(int x, int y) {
  static bool hh[3];
  hh[0] = hh[1] = hh[2] = 0;
  hh[x] = hh[y] = 1;
  for (int i = 0; i < 3; i++)
    if (!hh[i]) return i;
}
long long get(long long x, long long y) {
  return (x % mod + y % mod) % mod * ((y % mod - x % mod + 1 + mod) % mod) %
         mod * inv2 % mod;
}
int main() {
  scanf("%lld%lld", &n, &p);
  if (p <= 2) {
    puts("0");
    return 0;
  }
  t.push_back(th(1, 2, 0));
  t.push_back(th(3, 3, 1));
  t.push_back(th(4, 4, 2));
  t.push_back(th(5, 6, 1));
  int cnt = 0;
  while (t.back().r < p) {
    long long a1 = (t.back().r + 1) / 3;
    vector<th>::iterator it1 = lower_bound(t.begin(), t.end(), a1);
    long long a2 = t.back().r + 1 - a1;
    vector<th>::iterator it2 = lower_bound(t.begin(), t.end(), a2);
    long long b1 = it1->r, b2 = it2->r;
    int c = mex(it1->c, it2->c);
    long long r;
    if (b1 * 2 + 2 > b2) {
      b1 = b2 / 2;
      r = b1 + b2;
    } else
      r = b1 * 3 + 2;
    if (c == t.back().c)
      t.back().r = r;
    else
      t.push_back(th(t.back().r + 1, r, c));
    cnt++;
  }
  for (vector<th>::iterator it = t.begin(); it != t.end(); it++) {
    th x = *it;
    if (x.l >= p) break;
    (s[x.c] += get(max(1LL, p - x.r), p - x.l)) %= mod;
  }
  b.x = b.y = 4;
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++) b[i][j] = s[i ^ j];
  b = Pow(b, n);
  printf("%lld\n", ((b[0][1] + b[0][2]) % mod + b[0][3]) % mod);
}
