#include <bits/stdc++.h>
using namespace std;
const int p = 1e9 + 7, iv = 5e8 + 4;
struct node {
  int l, r, va;
} b[501200] = {(node){1, 2, 0},
               (node){3, 3, 1},
               (node){4, 4, 2},
               (node){5, 6, 1},
               (node){7, 8, 2},
               (node){9, 12, 0},
               (node){13, 14, 1},
               (node){15, 18, 2},
               (node){19, 26, 0},
               (node){27, 38, 1},
               (node){39, 39, 2},
               (node){40, 56, 0},
               (node){57, 57, 2},
               (node){58, 80, 1},
               (node){81, 84, 2},
               (node){85, 119, 0},
               (node){120, 120, 2},
               (node){121, 173, 1},
               (node){174, 178, 2},
               (node){179, 254, 0},
               (node){255, 259, 2},
               (node){260, 362, 1},
               (node){363, 381, 2},
               (node){382, 536, 0},
               (node){537, 543, 2},
               (node){544, 779, 1},
               (node){780, 804, 2},
               (node){805, 1145, 0},
               (node){1146, 1168, 2},
               (node){1169, 1631, 1},
               (node){1632, 1717, 2},
               (node){1718, 2414, 0},
               (node){2415, 2446, 2},
               (node){2447, 3506, 1},
               (node){3507, 3621, 2},
               (node){3622, 5153, 0},
               (node){5154, 5259, 2},
               (node){5260, 7340, 1},
               (node){7341, 7729, 2},
               (node){7730, 10865, 0},
               (node){10866, 11010, 2},
               (node){11011, 15779, 1},
               (node){15780, 16297, 2},
               (node){16298, 23189, 0},
               (node){23190, 23668, 2},
               (node){23669, 33032, 1},
               (node){33033, 34783, 2},
               (node){34784, 48893, 0},
               (node){48894, 49548, 2},
               (node){49549, 71006, 1},
               (node){71007, 73339, 2},
               (node){73340, 104351, 0},
               (node){104352, 106509, 2},
               (node){106510, 148646, 1},
               (node){148647, 156526, 2},
               (node){156527, 220019, 0},
               (node){220020, 222969, 2},
               (node){222970, 319529, 1},
               (node){319530, 330028, 2},
               (node){330029, 469580, 0},
               (node){469581, 479293, 2},
               (node){479294, 668909, 1},
               (node){668910, 704370, 2},
               (node){704371, 990086, 0},
               (node){990087, 1003363, 2},
               (node){1003364, 1437881, 1},
               (node){1437882, 1485129, 2},
               (node){1485130, 2113112, 0},
               (node){2113113, 2156821, 2},
               (node){2156822, 3010091, 1},
               (node){3010092, 3169668, 2},
               (node){3169669, 4455389, 0},
               (node){4455390, 4515136, 2},
               (node){4515137, 6470465, 1},
               (node){6470466, 6683083, 2},
               (node){6683084, 9509006, 0},
               (node){9509007, 9705697, 2},
               (node){9705698, 13545410, 1},
               (node){13545411, 14263509, 2},
               (node){14263510, 20049251, 0},
               (node){20049252, 20318115, 2},
               (node){20318116, 29117093, 1},
               (node){29117094, 30073876, 2},
               (node){30073877, 42790529, 0},
               (node){42790530, 43675639, 2},
               (node){43675640, 60954347, 1},
               (node){60954348, 64185793, 2},
               (node){64185794, 90221630, 0},
               (node){90221631, 91431520, 2},
               (node){91431521, 131026919, 1},
               (node){131026920, 135332445, 2},
               (node){135332446, 192557381, 0},
               (node){192557382, 196540378, 2},
               (node){196540379, 274294562, 1},
               (node){274294563, 288836071, 2},
               (node){288836072, 405997337, 0},
               (node){405997338, 411441843, 2},
               (node){411441844, 589621136, 1},
               (node){589621137, 608996005, 2},
               (node){608996006, 866508215, 0},
               (node){866508216, 884431704, 2},
               (node){884431705, 1000000000, 1}};
int n, m, sg[501200], tot, dp[2002][5];
bool vis[12];
inline int cal(int x) {
  int l = 1, r = tot, mid, ret;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (b[mid].r >= x)
      ret = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= 300; i++) {
    if (b[i].l <= m && b[i].l) {
      b[i].r = min(b[i].r, m);
      (dp[1][b[i].va] +=
       (1ll * m * (b[i].r - b[i].l + 1) % p -
        1ll * (b[i].l + b[i].r) * (b[i].r - b[i].l + 1) % p * iv % p + p) %
       p) %= p;
    }
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        (dp[i][j ^ k] += 1ll * dp[i - 1][j] * dp[1][k] % p) %= p;
  }
  int ans = 0;
  for (int i = 1; i <= 3; i++) ans = (ans + dp[n][i]) % p;
  printf("%d\n", ans);
}
