#include <bits/stdc++.h>
using namespace std;
const int N = 1005, mod = 1000000007;
int n, p, tot;
int sg[N], a[N], v[3], f[N][4];
int main() {
  scanf("%d%d", &n, &p);
  tot = 1;
  a[1] = 2;
  for (int i = 1, j = 1, p1, p2; a[tot] < p - 1; i += p1 <= p2, j += p2 <= p1) {
    p1 = a[i] * 3 + 2;
    p2 = a[j] + a[j] / 2;
    sg[++tot] = sg[i] && sg[j] ? 0 : (sg[i] + sg[j] == 1 ? 2 : 1);
    if (sg[tot] == sg[tot - 1]) tot--;
    a[tot] = min(p1, p2);
  }
  a[tot] = p - 1;
  for (int i = 1; i <= tot; i++)
    v[sg[i]] = (v[sg[i]] + (long long)(p - a[i] + p - a[i - 1] - 1) *
                               (a[i] - a[i - 1]) / 2) %
               mod;
  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] = ((long long)f[i - 1][j] * v[k] + f[i][j ^ k]) % mod;
  printf("%d\n", ((f[n][1] + f[n][2]) % mod + f[n][3]) % mod);
  return 0;
}
