#include <bits/stdc++.h>
const int A[103][2] = {
    {0, 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}, {1234325532, 2}};
inline long long getsum(int l, int r) {
  return 1LL * (l + r) * (r - l + 1LL) / 2LL;
};
inline void get_a(int *a, int n) {
  int last = 1, val = 0;
  int i;
  for (i = 1; i <= 102; i++) {
    int now = A[i][0];
    if (now > n) now = n + 1;
    a[val] = (a[val] + getsum(n - now + 1, n - last)) % 1000000007LL;
    last = now;
    val = A[i][1];
  };
};
int main() {
  int n, p;
  scanf("%d%d", &n, &p);
  int a[3] = {0, 0, 0};
  get_a(a, p);
  int f[4] = {1, 0, 0, 0};
  int pre[4];
  while (n--) {
    int i;
    for (i = 0; i < 4; i++) pre[i] = f[i];
    for (i = 0; i < 4; i++)
      f[i] = (1LL * pre[i] * a[0] + 1LL * pre[i ^ 1] * a[1] +
              1LL * pre[i ^ 2] * a[2]) %
             1000000007LL;
  };
  int ans = (1LL * f[1] + 1LL * f[2] + 1LL * f[3]) % 1000000007LL;
  printf("%d\n", ans);
}
