#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int n, p, a[1010], cnt;
int sg[1010], f[1010][4], s[4];
int main() {
  scanf("%d%d", &n, &p);
  a[cnt = 1] = min(p, 2);
  for (int l = 1, r = 1; a[cnt] < p;) {
    int ll = min(a[l] * 3ll + 2, 1ll * p), rr = min(a[r] * 3ll / 2, 1ll * p);
    int v = sg[l] && sg[r] ? 0 : sg[l] + sg[r] == 1 ? 2 : 1;
    if (v != sg[cnt]) sg[++cnt] = v;
    a[cnt] = min(ll, rr);
    l += (ll <= rr);
    r += (rr <= ll);
  }
  a[cnt] = p;
  for (int i = 1; i <= cnt; i++)
    s[sg[i]] = (s[sg[i]] +
                1ll * (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 < 4; j++)
      for (int k = 0; k <= 2; k++)
        f[i][j] = (f[i][j] + 1ll * f[i - 1][j ^ k] * s[k]) % MOD;
  printf("%d\n", ((f[n][1] + f[n][2]) % MOD + f[n][3]) % MOD);
  return 0;
}
