#include <bits/stdc++.h>
using namespace std;
const int mn = 1100, mo = (int)1e9 + 7;
int n, tot, p;
int a[mn], sg[mn], cnt[4];
int f[mn][4];
int calc(long long x) {
  int res = (p - 1 + p - x) * x / 2 % mo;
  return res;
}
int main() {
  scanf("%d%d", &n, &p);
  sg[0] = tot = 0, a[0] = 2;
  while (a[tot] < p) {
    int s = a[tot] + 1;
    int t1 = s / 3, t2 = s - s / 3, ng;
    t1 = lower_bound(a, a + 1 + tot, t1) - a;
    t2 = lower_bound(a, a + 1 + tot, t2) - a;
    a[++tot] = min(a[t1] * 3 + 2, a[t2] % 2 + 3 * (a[t2] / 2));
    int sg1 = sg[t1], sg2 = sg[t2];
    if (sg1 > sg2) swap(sg1, sg2);
    if (sg1 > 0)
      sg[tot] = 0;
    else if (sg2 == 1)
      sg[tot] = 2;
    else
      sg[tot] = 1;
    if (sg[tot] == sg[tot - 1]) {
      a[tot - 1] = a[tot];
      --tot;
    }
  }
  if (a[tot] > p) a[tot] = p;
  cnt[sg[0]] = calc(a[0]);
  int i, j, k;
  for (i = 1; i <= tot; ++i)
    cnt[sg[i]] = (cnt[sg[i]] + calc(a[i]) - calc(a[i - 1])) % mo;
  f[0][0] = 1;
  for (i = 1; i <= n; ++i)
    for (j = 0; j <= 3; ++j)
      for (k = 0; k <= 3; ++k)
        f[i][j ^ k] = (f[i][j ^ k] + (long long)f[i - 1][j] * cnt[k]) % mo;
  int res = ((f[n][1] + f[n][2]) % mo + f[n][3]) % mo;
  printf("%d\n", (res + mo) % mo);
  return 0;
}
