#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000000 + 7;
const int D[3][3] = {{1, 2, 1}, {2, 0, 0}, {1, 0, 0}};
deque<pair<long long, int> > s1, s2;
pair<pair<long long, long long>, int> seq[1010];
int sn;
int ta[3], f[1010][4];
inline long long max(long long a, long long b) { return a > b ? a : b; }
inline long long min(long long a, long long b) { return a < b ? a : b; }
inline void inc(int& a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
}
void add(const pair<pair<long long, long long>, int>& seg, long long bound) {
  if (seg.first.second * 3 + 2 >= bound) {
    if (!s1.empty() && s1.back().second == seg.second) s1.pop_back();
    s1.push_back(make_pair(seg.first.second * 3 + 2, seg.second));
  }
  if (seg.first.second * 3 / 2 >= bound) {
    if (!s2.empty() && s2.back().second == seg.second) s2.pop_back();
    s2.push_back(make_pair(seg.first.second * 3 / 2, seg.second));
  }
}
int main() {
  int n, p;
  scanf("%d%d", &n, &p);
  if (p < 3) {
    printf("0\n");
    return 0;
  }
  seq[sn++] = make_pair(make_pair(1, 2), 0);
  add(seq[0], 3);
  long long bound = 3;
  while (bound <= p) {
    while (s1.front().first < bound) s1.pop_front();
    while (s2.front().first < bound) s2.pop_front();
    long long r = min(s1.front().first, s2.front().first);
    if (r > p) r = p;
    seq[sn++] =
        make_pair(make_pair(bound, r), D[s1.front().second][s2.front().second]);
    bound = r + 1;
    add(seq[sn - 1], bound);
  }
  for (int i = 0; i < sn; i++) {
    int id = seq[i].second;
    int l = (int)seq[i].first.first;
    int r = (int)seq[i].first.second;
    ta[id] += (int)(((long long)p - l + p - r) * (r - l + 1) / 2 % MOD);
    if (ta[id] >= MOD) ta[id] -= MOD;
  }
  f[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 3; k++)
        inc(f[i][j ^ k], (int)((long long)f[i - 1][j] * ta[k] % MOD));
  int ans = 0;
  for (int i = 1; i < 4; i++) inc(ans, f[n][i]);
  printf("%d\n", ans);
  return 0;
}
