#include <bits/stdc++.h>
void Get(long long &T) {
  char C;
  bool F = 0;
  for (; C = getchar(), C < '0' || C > '9';)
    if (C == '-') F = 1;
  for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0')
    ;
  F && (T = -T);
}
int SG[1000004];
long long Cnt[5];
long long F[1005][5];
int Trans[5][5];
long long L[5005];
long long R[5005];
int Val[5005];
int Top;
long long N, M;
long long Ans;
void Init() {
  Get(N);
  Get(M);
}
void Work() {
  Trans[0][0] = 1;
  Trans[0][1] = 2;
  Trans[0][2] = 1;
  Trans[1][0] = 2;
  Trans[1][1] = 0;
  Trans[1][2] = 0;
  Trans[2][0] = 1;
  Trans[2][1] = 0;
  Trans[2][2] = 0;
  Top = 1;
  L[1] = 1;
  R[1] = 2;
  Val[1] = 0;
  int p = 1, q = 1;
  for (long long i = 3;;) {
    if (i > 2000000000) break;
    long long End1 = R[p] * 3 + 2;
    long long End2;
    if (R[q] & 1) {
      End2 = (R[q] - 1) / 2 * 3 + 1;
    } else
      End2 = R[q] / 2 * 3;
    long long End = End1;
    if (End > End2) End = End2;
    Top++;
    L[Top] = i;
    R[Top] = End;
    Val[Top] = Trans[Val[p]][Val[q]];
    if (End == End1) p++;
    if (End == End2) q++;
    i = End + 1;
    if (Top > 1 && Val[Top] == Val[Top - 1]) {
      R[Top - 1] = R[Top];
      Top--;
    }
  }
  for (int i = 1; i <= Top; i++) {
    if (L[i] > M) break;
    if (R[i] > M) R[i] = M;
    (Cnt[Val[i]] += M * (R[i] - L[i] + 1)) %= (1000000007ll);
    (Cnt[Val[i]] -= (((R[i] + L[i]) * (R[i] - L[i] + 1)) >> 1)) %=
        (1000000007ll);
  }
  F[0][0] = 1;
  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= 3; j++)
      for (int k = 0; k <= 2; k++)
        (F[i][j ^ k] += F[i - 1][j] * Cnt[k]) %= (1000000007ll);
  Ans = (F[N][1] + F[N][2] + F[N][3]) % (1000000007ll);
  if (Ans < 0) Ans += (1000000007ll);
}
void Output() { printf("%I64d\n", Ans); }
int main() {
  Init();
  Work();
  Output();
  return 0;
}
