#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, p;
  cin >> n >> p;
  if (p <= 2) {
    cout << 0;
    return 0;
  }
  vector<pair<long long, int> > a;
  a.push_back(pair<long long, int>(2, 0));
  a.push_back(pair<long long, int>(3, 1));
  int i(0), j(1);
  long long k(3), b, c;
  while (k < p) {
    b = 3 * a[i].first + 2;
    c = 3 * (a[j].first / 2) + a[j].first % 2;
    k = min(b, c);
    if (a[i].second != 0 && a[j].second != 0) {
      if (a.back().second == 0)
        a.back().first = k;
      else
        a.push_back(pair<long long, int>(k, 0));
    } else if (a[i].second != 1 && a[j].second != 1) {
      if (a.back().second == 1)
        a.back().first = k;
      else
        a.push_back(pair<long long, int>(k, 1));
    } else {
      if (a.back().second == 2)
        a.back().first = k;
      else
        a.push_back(pair<long long, int>(k, 2));
    }
    if (b <= c) ++i;
    if (c <= b) ++j;
  }
  long long nimble[4] = {0};
  for (int i = a.size() - 1; i > 0; --i)
    nimble[a[i].second] += (a[i].first - a[i - 1].first) *
                           (2 * p - a[i].first - a[i - 1].first - 1) / 2;
  nimble[a.back().second] -=
      (a.back().first - p) * (p - a.back().first - 1) / 2;
  nimble[0] += 2 * p - 3;
  for (int i = 0; i < 4; ++i) nimble[i] %= mod;
  long long tmp[4], ans[4] = {1, 0, 0, 0};
  for (int i = n; i > 0; i /= 2) {
    if (i % 2) {
      for (int j = 0; j < 4; ++j) tmp[j] = ans[j];
      for (int j = 0; j < 4; ++j) {
        ans[j] = 0;
        for (int k = 0; k < 4; ++k) ans[j] += tmp[k] * nimble[j ^ k];
        ans[j] %= mod;
      }
    }
    for (int j = 0; j < 4; ++j) tmp[j] = nimble[j];
    for (int j = 0; j < 4; ++j) {
      nimble[j] = 0;
      for (int k = 0; k < 4; ++k) nimble[j] += tmp[k] * tmp[j ^ k];
      nimble[j] %= mod;
    }
  }
  cout << (ans[1] + ans[2] + ans[3]) % mod << endl;
  return 0;
}
