#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int tab[105][2] = {
    {1, 0},         {3, 1},         {4, 2},         {5, 1},
    {7, 2},         {9, 0},         {13, 1},        {15, 2},
    {19, 0},        {27, 1},        {39, 2},        {40, 0},
    {57, 2},        {58, 1},        {81, 2},        {85, 0},
    {120, 2},       {121, 1},       {174, 2},       {179, 0},
    {255, 2},       {260, 1},       {363, 2},       {382, 0},
    {537, 2},       {544, 1},       {780, 2},       {805, 0},
    {1146, 2},      {1169, 1},      {1632, 2},      {1718, 0},
    {2415, 2},      {2447, 1},      {3507, 2},      {3622, 0},
    {5154, 2},      {5260, 1},      {7341, 2},      {7730, 0},
    {10866, 2},     {11011, 1},     {15780, 2},     {16298, 0},
    {23190, 2},     {23669, 1},     {33033, 2},     {34784, 0},
    {48894, 2},     {49549, 1},     {71007, 2},     {73340, 0},
    {104352, 2},    {106510, 1},    {148647, 2},    {156527, 0},
    {220020, 2},    {222970, 1},    {319530, 2},    {330029, 0},
    {469581, 2},    {479294, 1},    {668910, 2},    {704371, 0},
    {990087, 2},    {1003364, 1},   {1437882, 2},   {1485130, 0},
    {2113113, 2},   {2156822, 1},   {3010092, 2},   {3169669, 0},
    {4455390, 2},   {4515137, 1},   {6470466, 2},   {6683084, 0},
    {9509007, 2},   {9705698, 1},   {13545411, 2},  {14263510, 0},
    {20049252, 2},  {20318116, 1},  {29117094, 2},  {30073877, 0},
    {42790530, 2},  {43675640, 1},  {60954348, 2},  {64185794, 0},
    {90221631, 2},  {91431521, 1},  {131026920, 2}, {135332446, 0},
    {192557382, 2}, {196540379, 1}, {274294563, 2}, {288836072, 0},
    {405997338, 2}, {411441844, 1}, {589621137, 2}, {608996006, 0},
    {866508216, 2}, {884431705, 1}, {1000000001, 1}};
long long cnt[4];
long long dp[1010][4];
int main() {
  ios::sync_with_stdio(0);
  memset(cnt, 0, sizeof(cnt));
  int n, p;
  cin >> n >> p;
  for (int t = 0;; t++) {
    if (tab[t + 1][0] >= p) {
      cnt[tab[t][1]] +=
          (long long)(2 * p - tab[t][0] - p + 1) * (p - tab[t][0]) / 2;
      break;
    }
    cnt[tab[t][1]] += (long long)(2 * p - tab[t][0] - tab[t + 1][0] + 1) *
                      (tab[t + 1][0] - tab[t][0]) / 2;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < 4; i++) {
    cnt[i] %= mod;
    dp[1][i] = cnt[i];
  }
  for (int i = 2; i <= n; i++) {
    dp[i][0] = (dp[i - 1][0] * cnt[0] % mod + dp[i - 1][1] * cnt[1] % mod +
                dp[i - 1][2] * cnt[2] % mod) %
               mod;
    dp[i][1] = (dp[i - 1][0] * cnt[1] % mod + dp[i - 1][1] * cnt[0] % mod +
                dp[i - 1][3] * cnt[2] % mod) %
               mod;
    dp[i][2] = (dp[i - 1][0] * cnt[2] % mod + dp[i - 1][2] * cnt[0] % mod +
                dp[i - 1][3] * cnt[1] % mod) %
               mod;
    dp[i][3] = (dp[i - 1][1] * cnt[2] % mod + dp[i - 1][2] * cnt[1] % mod +
                dp[i - 1][3] * cnt[0] % mod) %
               mod;
  }
  cout << (dp[n][1] + dp[n][2] + dp[n][3]) % mod << endl;
  return 0;
}
