#include <bits/stdc++.h>
inline int Get() {
  char ch;
  while ((ch = getchar()) < '0' || ch > '9')
    ;
  int Num = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    Num = (Num << 3) + (Num << 1) + ch - '0';
  return Num;
}
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, m, count[3], xorn[2][4];
int len, l[N], r[N], sg[N];
int main() {
  n = Get(), m = Get();
  len = 1, l[1] = 1, r[1] = 2, sg[1] = 0;
  int a = 1, b = 1;
  while (r[len] < m) {
    while (r[a] * 3 + 2 <= r[len]) ++a;
    while (r[b] / 2 * 3 + (r[b] & 1) <= r[len]) ++b;
    l[len + 1] = r[len] + 1;
    r[++len] = std ::min(r[a] * 3 + 2, r[b] / 2 * 3 + (r[b] & 1));
    if (!sg[a] || !sg[b]) sg[len] = 1 + (sg[a] == 1 || sg[b] == 1);
    if (sg[len] == sg[len - 1]) r[len - 1] = r[len], --len;
  }
  for (int i = 1; i <= len; ++i) {
    r[i] = std ::min(r[i], m - 1);
    count[sg[i]] = (count[sg[i]] +
                    (long long)(r[i] - l[i] + 1) * (m * 2 - l[i] - r[i]) / 2) %
                   mod;
  }
  int x = 0, y = 1;
  xorn[0][0] = 1;
  for (int i = 1; i <= n; ++i, std ::swap(x, y)) {
    for (int k = 0; k < 4; ++k) xorn[y][k] = 0;
    for (int k = 0; k < 4; ++k)
      for (int j = 0; j < 3; ++j)
        xorn[y][j ^ k] =
            ((long long)xorn[x][k] * count[j] + xorn[y][j ^ k]) % mod;
  }
  printf("%d\n", ((long long)xorn[x][1] + xorn[x][2] + xorn[x][3]) % mod);
}
