#include <bits/stdc++.h>
using namespace std;
bool endline = false;
template <class T>
istream& operator>>(istream& inp, vector<T>& v) {
  for (auto& it : v) inp >> it;
  return inp;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (auto& it : v) out << it << (endline ? "\n" : " ");
  return out;
}
template <class T, class U>
istream& operator>>(istream& inp, pair<T, U>& v) {
  inp >> v.first >> v.second;
  return inp;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U>& v) {
  out << v.first << ' ' << v.second;
  return out;
}
void debug() { cout << endl; }
template <typename H, typename... T>
void debug(H a, T... b) {
  cout << a << ' ';
  debug(b...);
}
void FAST_IO(string filein = "", string fileout = "", string fileerr = "") {
  if (fopen(filein.c_str(), "r")) {
    freopen(filein.c_str(), "r", stdin);
    freopen(fileout.c_str(), "w", stdout);
  }
  cin.tie(0), cout.tie(0)->sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main() {
  FAST_IO(".inp", ".out");
  int testcases = 1;
  for (int test = 1; test <= testcases; test++) {
    Hollwo_Pelw();
  }
  return 0;
}
const int allmod[3] = {(int)1e9 + 7, 998244353, (int)1e9 + 9};
const int mod = allmod[0];
const int MAXN = 2020;
const int inf = 2e9;
const long long linf = 1e18;
const long double eps = 1e-9;
long long l[MAXN], r[MAXN], g[MAXN], n, p, cnt[MAXN], dp[MAXN][4], idx;
inline long long mex(long long a, long long b) {
  for (long long i = 0;; i++)
    if (i != a && i != b) return i;
}
void sadd(long long& a, long long b) { a = (a + b % mod) % mod; }
void __pre_calc__() {
  l[0] = 1, r[0] = 3, g[0] = 0;
  while (1) {
    if (r[idx] > p) {
      r[idx] = p + 1;
      break;
    }
    ++idx;
    l[idx] = r[idx - 1];
    long long len = inf, v1, v2;
    {
      long long to = l[idx] / 3, pos = 0;
      while (to >= r[pos] && pos < idx) pos++;
      len = min(len, r[pos] - to), v1 = g[pos];
    }
    {
      long long to = l[idx] - l[idx] / 3, pos = 0;
      while (to >= r[pos] && pos < idx) pos++;
      len = min(len, r[pos] - to), v2 = g[pos];
    }
    r[idx] = l[idx] + len;
    g[idx] = mex(v1, v2);
    if (g[idx] == g[idx - 1]) r[idx - 1] = r[idx], idx--;
  }
  for (long long i = 0; i <= idx; i++)
    sadd(cnt[g[i]], (2 * p - l[i] - r[i] + 1) * (r[i] - l[i]) / 2);
}
void Hollwo_Pelw() {
  cin >> n >> p;
  __pre_calc__();
  dp[0][0] = 1;
  for (long long i = 1; i <= n; i++)
    for (long long j = 0; j <= 3; j++)
      for (long long k = 0; k <= 3; k++) {
        sadd(dp[i][j], dp[i - 1][k] * cnt[k ^ j]);
      }
  long long ans = 0;
  for (long long i = 1; i <= 3; i++) sadd(ans, dp[n][i]);
  cout << ans;
}
