#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1005;
const long long MOD = 1000000007;
long long dp[MAXN][4];
long long cnt[4];
long long n, p;
void load() { cin >> n >> p; }
long long mex(long long a, long long b) {
  for (long long i = 0; i < 3; ++i)
    if (a != i && b != i) return i;
}
long long x[MAXN], sg[MAXN];
long long sum(long long a, long long b) {
  long long l = p - b, r = p - a;
  return ((l + r) * (r - l + 1) / 2) % MOD;
}
void calc() {
  long long num = 1;
  x[num] = 2, sg[num] = 0;
  long long l = 1, r = 1;
  while (x[num] < p) {
    long long nl = (x[l] + 1) * 3 - 1;
    long long nr = x[r] / 2 * 3;
    if (x[r] & 1) ++nr;
    sg[++num] = mex(sg[l], sg[r]);
    if (sg[num] == sg[num - 1]) --num;
    x[num] = min(nl, nr);
    if (nl == x[num]) ++l;
    if (nr == x[num]) ++r;
  }
  if (x[num] >= p) x[num] = p - 1;
  for (long long i = 1; i <= num; ++i)
    cnt[sg[i]] = (cnt[sg[i]] + sum(x[i - 1] + 1, x[i])) % MOD;
}
void work() {
  calc();
  dp[0][0] = 1;
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < 4; ++j) {
      for (long long k = 0; k < 4; ++k) {
        long long t = j ^ k;
        dp[i + 1][t] = (dp[i][j] * cnt[k] + dp[i + 1][t]) % MOD;
      }
    }
  }
  long long ans = 0;
  for (long long i = 1; i < 4; ++i) ans = (dp[n][i] + ans) % MOD;
  cout << ans << endl;
}
int main() {
  load();
  work();
  return 0;
}
