#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
const long long int MOD = 1e9 + 696969;
const long long INF = 4LL * 1000000LL * 1000000LL * 1000000LL;
template <class T>
int size(T &&x) {
  return int(x.size());
}
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  out << '{';
  for (auto it = x.begin(); it != x.end(); ++it)
    out << *it << (it == prev(x.end()) ? "" : ", ");
  return out << '}';
}
void dump() {}
template <class T, class... Args>
void dump(T &&x, Args... args) {
  cerr << x << ";  ";
  dump(args...);
}
mt19937_64 rng(0);
int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
inline void addmod(int &x, int val) {
  x += val;
  while (x >= mod) x -= mod;
}
const int maxn = 1000100;
int nim[100100], N = 100000;
int jest[3];
int tab[1010][1010];
vector<pair<int, int> > res;
vector<int> vals;
inline int getNim(int value, int curr_expanding) {
  if (value > res.back().second) return curr_expanding;
  int x = 0, y = res.size() - 1;
  while (x < y) {
    int sr = (x + y) / 2;
    if (res[sr].first <= value && value <= res[sr].second) return vals[sr];
    if (value < res[sr].first)
      y = --sr;
    else
      x = ++sr;
  }
  return vals[x];
}
inline int findNim(int length, int curr_expanding) {
  for (int j = (0); j <= (2); ++j) jest[j] = false;
  int val = length / 3;
  jest[getNim(length - val, curr_expanding)] = true;
  jest[getNim(val, curr_expanding)] = true;
  int nm = 0;
  while (jest[nm]) ++nm;
  return nm;
}
const int MAXP = 1000;
void expand() {
  int next_point = res.back().second + 1;
  int curr_value = findNim(next_point, 0);
  int gn = -1;
  for (int pt = 1; next_point + pt - 1 <= (int)1e9; pt *= 2) {
    int query_point = next_point + pt - 1;
    int value = findNim(query_point, curr_value);
    if (value != curr_value) {
      gn = query_point;
      break;
    }
  }
  if (gn == -1) {
    res.push_back({next_point, (int)1e9});
    vals.push_back(curr_value);
    return;
  }
  int binx = next_point, biny = gn;
  while (binx < biny) {
    int sr = (binx + biny) / 2 + 1;
    if (findNim(sr, curr_value) != curr_value)
      biny = --sr;
    else
      binx = sr;
  }
  res.push_back({next_point, binx});
  vals.push_back(curr_value);
  return;
}
long long ways[4];
long long dp[1010][4];
int n, p;
int main() {
  nim[0] = nim[1] = nim[2] = 0;
  for (int i = 3; i <= N; ++i) {
    int val = (i / 3);
    for (int j = (0); j <= (2); ++j) jest[j] = false;
    jest[nim[i - val]] = true;
    jest[nim[val]] = true;
    int nm = 0;
    while (jest[nm]) ++nm;
    nim[i] = nm;
  }
  res = {};
  vector<int> lengths = {};
  vals = {};
  int pos = 0;
  for (int i = (1); i <= (N); ++i) {
    if (nim[pos] != nim[i]) {
      res.push_back({pos, i - 1});
      vals.push_back(nim[pos]);
      lengths.push_back(res.back().second - res.back().first + 1);
      pos = i;
    }
  }
  res.pop_back();
  res.pop_back();
  vals.pop_back();
  vals.pop_back();
  int tr = 0;
  while (res.back().second != (int)1e9) {
    expand();
  }
  res[0].first++;
  cin >> n >> p;
  if (p == 1) {
    cout << 0;
    exit(0);
  };
  --p;
  int sz = res.size();
  for (int i = 0; i < sz; ++i) {
    pair<int, int> u = res[i];
    if (p < u.first) break;
    int max_val = min(p, u.second), min_val = u.first;
    long long leng = max_val - min_val + 1;
    long long pocz = (p - min_val + 1), kon = (p - max_val + 1);
    long long val = (leng * (pocz + kon)) / 2;
    ways[vals[i]] += val % mod;
  }
  for (int i = (0); i <= (3); ++i) ways[i] %= mod;
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 4; ++j)
      for (int k = 0; k <= 2; ++k) {
        dp[i][(j ^ k)] += dp[i - 1][j] * ways[k];
        dp[i][(j ^ k)] %= mod;
      }
  }
  long long solve = 0;
  for (int i = (1); i <= (3); ++i) solve += dp[n][i];
  solve %= mod;
  cout << solve;
}

### Prompt

Generate a cpp solution to the following problem:

Dima and Anya love playing different games. Now Dima has imagined a new game that he wants to play with Anya.

Dima writes n pairs of integers on a piece of paper (li, ri) (1 ≤ li < ri ≤ p). Then players take turns. On his turn the player can do the following actions:

  1. choose the number of the pair i (1 ≤ i ≤ n), such that ri - li > 2; 
  2. replace pair number i by pair <image> or by pair <image>. Notation ⌊x⌋ means rounding down to the closest integer. 



The player who can't make a move loses.

Of course, Dima wants Anya, who will move first, to win. That's why Dima should write out such n pairs of integers (li, ri) (1 ≤ li < ri ≤ p), that if both players play optimally well, the first one wins. Count the number of ways in which Dima can do it. Print the remainder after dividing the answer by number 1000000007 (109 + 7).

Two ways are considered distinct, if the ordered sequences of the written pairs are distinct.

Input

The first line contains two integers n, p (1 ≤ n ≤ 1000, 1 ≤ p ≤ 109). The numbers are separated by a single space.

Output

In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7).

Examples

Input

2 2


Output

0


Input

4 4


Output

520


Input

100 1000


Output

269568947

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
const long long int MOD = 1e9 + 696969;
const long long INF = 4LL * 1000000LL * 1000000LL * 1000000LL;
template <class T>
int size(T &&x) {
  return int(x.size());
}
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  out << '{';
  for (auto it = x.begin(); it != x.end(); ++it)
    out << *it << (it == prev(x.end()) ? "" : ", ");
  return out << '}';
}
void dump() {}
template <class T, class... Args>
void dump(T &&x, Args... args) {
  cerr << x << ";  ";
  dump(args...);
}
mt19937_64 rng(0);
int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
inline void addmod(int &x, int val) {
  x += val;
  while (x >= mod) x -= mod;
}
const int maxn = 1000100;
int nim[100100], N = 100000;
int jest[3];
int tab[1010][1010];
vector<pair<int, int> > res;
vector<int> vals;
inline int getNim(int value, int curr_expanding) {
  if (value > res.back().second) return curr_expanding;
  int x = 0, y = res.size() - 1;
  while (x < y) {
    int sr = (x + y) / 2;
    if (res[sr].first <= value && value <= res[sr].second) return vals[sr];
    if (value < res[sr].first)
      y = --sr;
    else
      x = ++sr;
  }
  return vals[x];
}
inline int findNim(int length, int curr_expanding) {
  for (int j = (0); j <= (2); ++j) jest[j] = false;
  int val = length / 3;
  jest[getNim(length - val, curr_expanding)] = true;
  jest[getNim(val, curr_expanding)] = true;
  int nm = 0;
  while (jest[nm]) ++nm;
  return nm;
}
const int MAXP = 1000;
void expand() {
  int next_point = res.back().second + 1;
  int curr_value = findNim(next_point, 0);
  int gn = -1;
  for (int pt = 1; next_point + pt - 1 <= (int)1e9; pt *= 2) {
    int query_point = next_point + pt - 1;
    int value = findNim(query_point, curr_value);
    if (value != curr_value) {
      gn = query_point;
      break;
    }
  }
  if (gn == -1) {
    res.push_back({next_point, (int)1e9});
    vals.push_back(curr_value);
    return;
  }
  int binx = next_point, biny = gn;
  while (binx < biny) {
    int sr = (binx + biny) / 2 + 1;
    if (findNim(sr, curr_value) != curr_value)
      biny = --sr;
    else
      binx = sr;
  }
  res.push_back({next_point, binx});
  vals.push_back(curr_value);
  return;
}
long long ways[4];
long long dp[1010][4];
int n, p;
int main() {
  nim[0] = nim[1] = nim[2] = 0;
  for (int i = 3; i <= N; ++i) {
    int val = (i / 3);
    for (int j = (0); j <= (2); ++j) jest[j] = false;
    jest[nim[i - val]] = true;
    jest[nim[val]] = true;
    int nm = 0;
    while (jest[nm]) ++nm;
    nim[i] = nm;
  }
  res = {};
  vector<int> lengths = {};
  vals = {};
  int pos = 0;
  for (int i = (1); i <= (N); ++i) {
    if (nim[pos] != nim[i]) {
      res.push_back({pos, i - 1});
      vals.push_back(nim[pos]);
      lengths.push_back(res.back().second - res.back().first + 1);
      pos = i;
    }
  }
  res.pop_back();
  res.pop_back();
  vals.pop_back();
  vals.pop_back();
  int tr = 0;
  while (res.back().second != (int)1e9) {
    expand();
  }
  res[0].first++;
  cin >> n >> p;
  if (p == 1) {
    cout << 0;
    exit(0);
  };
  --p;
  int sz = res.size();
  for (int i = 0; i < sz; ++i) {
    pair<int, int> u = res[i];
    if (p < u.first) break;
    int max_val = min(p, u.second), min_val = u.first;
    long long leng = max_val - min_val + 1;
    long long pocz = (p - min_val + 1), kon = (p - max_val + 1);
    long long val = (leng * (pocz + kon)) / 2;
    ways[vals[i]] += val % mod;
  }
  for (int i = (0); i <= (3); ++i) ways[i] %= mod;
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 4; ++j)
      for (int k = 0; k <= 2; ++k) {
        dp[i][(j ^ k)] += dp[i - 1][j] * ways[k];
        dp[i][(j ^ k)] %= mod;
      }
  }
  long long solve = 0;
  for (int i = (1); i <= (3); ++i) solve += dp[n][i];
  solve %= mod;
  cout << solve;
}

```