#include <bits/stdc++.h>
using namespace std;
const int MODULE = (int)1e9 + 7;
int cnt[3];
int ql[2222222];
int qr[2222222];
int qans[2222222];
int Nanase(int p) {
  int low = 0;
  int high = 0;
  int end = 0;
  ql[end] = 0;
  qr[end] = 2;
  qans[end++] = 0;
  cnt[0] = (cnt[0] + 2LL * p - 3) % MODULE;
  for (int i = 3; i < p;) {
    while (low < end && qr[low] < i / 3) low++;
    while (high < end && qr[high] < i - i / 3) high++;
    unsigned int sved = 0;
    unsigned int q = qr[high];
    for (int j = 0; j < 3; j++) {
      if ((3U * q - j) % 2) continue;
      if ((3U * q - j) / 2 % 3 != j) continue;
      sved = max(sved, (3U * q - j) / 2);
    }
    unsigned int till = min<int>(min(qr[low] * 3U + 2, sved), p - 1);
    int mexa[3] = {0};
    mexa[qans[low]]++;
    mexa[qans[high]]++;
    for (int j = 0; j < 3; j++) {
      if (!mexa[j]) {
        if (end && j == qans[end - 1])
          qr[end - 1] = till;
        else {
          ql[end] = i;
          qr[end] = till;
          qans[end++] = j;
        }
        int len = till - i + 1;
        cnt[j] += ((long long)p * len % MODULE -
                   (long long)(i + till) * len / 2 % MODULE + MODULE) %
                  MODULE;
        cnt[j] %= MODULE;
        break;
      }
    }
    i = till + 1;
  }
  return end;
}
int F[1111][4];
int main(void) {
  int n = 0;
  int p = 0;
  scanf("%d %d", &n, &p);
  Nanase(p);
  F[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 3; k++) {
        F[i + 1][j ^ k] =
            (F[i + 1][j ^ k] + (long long)F[i][j] * cnt[k]) % MODULE;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i < 4; i++) ans = (ans + F[n][i]) % MODULE;
  printf("%d\n", ans);
  while (getchar() != EOF)
    ;
  return 0;
}
