#include <bits/stdc++.h>
using namespace std;
namespace Dango {
const int MAXN = 1005, MOD = 1000000007;
int n;
long long p;
int border[MAXN * 10], val[MAXN * 10], cnt;
long long dp[MAXN][4], tot[3];
long long ans;
int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= (ch == '-');
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f ? -x : x;
}
long long pow_(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
int work() {
  n = read();
  p = read();
  cnt++;
  border[cnt] = 2;
  val[cnt] = 0;
  int pos = 3, l = 1, r = 1;
  while (pos < p) {
    long long ll = border[r] & 1 ? 1ll * (border[r] - 1) / 2 * 3 + 1
                                 : 1ll * border[r] / 2 * 3;
    long long rr = 1ll * border[l] * 3 + 2;
    cnt++;
    for (int i = 0; i <= 2; i++)
      if (i != val[l] && i != val[r]) {
        val[cnt] = i;
        break;
      }
    if (val[cnt] == val[cnt - 1]) cnt--;
    border[cnt] = min(ll, rr);
    if (border[cnt] == ll) r++;
    if (border[cnt] == rr) l++;
    pos = border[cnt] + 1;
  }
  for (int i = 1; i <= cnt; i++) {
    long long ll = border[i - 1] + 1, rr = min((long long)border[i], p);
    tot[val[i]] =
        (tot[val[i]] + (p - ll + p - rr) * (rr - ll + 1) / 2 % MOD) % MOD;
  }
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 3; j++)
      for (int k = 0; k <= 2; k++)
        dp[i][j] = (dp[i][j] + dp[i - 1][j ^ k] * tot[k] % MOD) % MOD;
  ans = (dp[n][1] + dp[n][2] + dp[n][3]) % MOD;
  printf("%lld", ans);
  return 0;
}
}  // namespace Dango
int main() { return Dango::work(); }
