#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
template <class T>
inline void read(T& x, T& y) {
  read(x);
  read(y);
}
template <class T>
inline void read(T& x, T& y, T& z) {
  read(x);
  read(y);
  read(z);
}
inline char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
const int mod = int(1e9) + 7;
int n, p, i, j, k, L, R;
long long f[1010][5];
long long g[5];
map<int, int> S;
map<int, int>::iterator it, it2;
void pre() {
  S[1] = 0;
  S[3] = 1;
  S[4] = 2;
  S[5] = 1;
  S[7] = 2;
  S[9] = 0;
  S[13] = 1;
  S[15] = 2;
  S[19] = 0;
  S[27] = 1;
  S[39] = 2;
  S[40] = 0;
  S[57] = 2;
  S[58] = 1;
  S[81] = 2;
  S[85] = 0;
  S[120] = 2;
  S[121] = 1;
  S[174] = 2;
  S[179] = 0;
  S[255] = 2;
  S[260] = 1;
  S[363] = 2;
  S[382] = 0;
  S[537] = 2;
  S[544] = 1;
  S[780] = 2;
  S[805] = 0;
  S[1146] = 2;
  S[1169] = 1;
  S[1632] = 2;
  S[1718] = 0;
  S[2415] = 2;
  S[2447] = 1;
  S[3507] = 2;
  S[3622] = 0;
  S[5154] = 2;
  S[5260] = 1;
  S[7341] = 2;
  S[7730] = 0;
  S[10866] = 2;
  S[11011] = 1;
  S[15780] = 2;
  S[16298] = 0;
  S[23190] = 2;
  S[23669] = 1;
  S[33033] = 2;
  S[34784] = 0;
  S[48894] = 2;
  S[49549] = 1;
  S[71007] = 2;
  S[73340] = 0;
  S[104352] = 2;
  S[106510] = 1;
  S[148647] = 2;
  S[156527] = 0;
  S[220020] = 2;
  S[222970] = 1;
  S[319530] = 2;
  S[330029] = 0;
  S[469581] = 2;
  S[479294] = 1;
  S[668910] = 2;
  S[704371] = 0;
  S[990087] = 2;
  S[1003364] = 1;
  S[1437882] = 2;
  S[1485130] = 0;
  S[2113113] = 2;
  S[2156822] = 1;
  S[3010092] = 2;
  S[3169669] = 0;
  S[4455390] = 2;
  S[4515137] = 1;
  S[6470466] = 2;
  S[6683084] = 0;
  S[9509007] = 2;
  S[9705698] = 1;
  S[13545411] = 2;
  S[14263510] = 0;
  S[20049252] = 2;
  S[20318116] = 1;
  S[29117094] = 2;
  S[30073877] = 0;
  S[42790530] = 2;
  S[43675640] = 1;
  S[60954348] = 2;
  S[64185794] = 0;
  S[90221631] = 2;
  S[91431521] = 1;
  S[131026920] = 2;
  S[135332446] = 0;
  S[192557382] = 2;
  S[196540379] = 1;
  S[274294563] = 2;
  S[288836072] = 0;
  S[405997338] = 2;
  S[411441844] = 1;
  S[589621137] = 2;
  S[608996006] = 0;
  S[866508216] = 2;
  S[884431705] = 1;
}
long long calc(long long x, long long y) {
  long long r = p - x, l = p - y;
  return (l + r) * (r - l + 1) / 2 % mod;
}
int main() {
  pre();
  read(n, p);
  for (it = S.begin(); it != S.end(); it++) {
    it2 = it;
    it2++;
    L = it->first;
    if (L > p) break;
    if (it2 == S.end() || it2->first - 1 >= p)
      R = p;
    else
      R = it2->first - 1;
    (g[it->second] += calc(L, R)) %= mod;
  }
  f[0][0] = 1;
  for (i = 1; i <= n; i++)
    for (j = 0; j <= 3; j++)
      for (k = 0; k <= 3; k++)
        f[i][j] = (f[i][j] + f[i - 1][j ^ k] * g[k]) % mod;
  long long ans = (f[n][1] + f[n][2] + f[n][3]) % mod;
  if (ans < 0) ans += mod;
  cout << ans << endl;
  scanf("\n");
  return 0;
}
