#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 1005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
  string s = "/home/jslijin/jslijin/code/";
  freopen((s + "code.in").c_str(), "r", stdin);
  freopen((s + "code.out").c_str(), "w", stdout);
}
struct T {
  int l, r, x;
};
vector<T> V;
int n, p, L, R;
long long dp[N][4], cnt[3];
long long G(long long l, long long r) {
  return (r - l + 1) * (2 * p - l - r) / 2 % _p;
}
bool check(int k, int a, int b) { return k / 3 > V[a].r || k - k / 3 > V[b].r; }
int F(int x, int y) {
  int ret = 0;
  ret |= 1 << x, ret |= 1 << y;
  return __builtin_ctz(++ret);
}
int main() {
  scanf("%d%d", &n, &p), V.push_back(T{1, 2, 0}), L = 3;
  while (L <= p) {
    int x = L / 3, y = L - x, a, b;
    for (a = 0; a < V.size(); ++a)
      if (V[a].l <= x && x <= V[a].r) break;
    for (b = 0; b < V.size(); ++b)
      if (V[b].l <= y && y <= V[b].r) break;
    int LL = L, RR = p + 1, ans = 0;
    while (LL <= RR) {
      int mid = (long long)(LL + RR) >> 1;
      if (check(mid, a, b))
        ans = mid, RR = mid - 1;
      else
        LL = mid + 1;
    }
    ans--;
    if (ans == -1) ans = p;
    int k = F(V[a].x, V[b].x);
    if (!V.empty() && V.back().x == k) L = V.back().l, V.pop_back();
    V.push_back(T{L, ans, k}), L = ans + 1;
  }
  for (auto t : V) cnt[t.x] += G(t.l, t.r), cnt[t.x] %= _p;
  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 - 1][j ^ k] * cnt[k] % _p, dp[i][j] %= _p;
  printf("%lld\n", (dp[n][1] + dp[n][2] + dp[n][3]) % _p);
  return 0;
}
