You are running through a rectangular field. This field can be represented as a matrix with 3 rows and m columns. (i, j) denotes a cell belonging to i-th row and j-th column.

You start in (2, 1) and have to end your path in (2, m). From the cell (i, j) you may advance to:

  * (i - 1, j + 1) — only if i > 1, 
  * (i, j + 1), or 
  * (i + 1, j + 1) — only if i < 3. 



However, there are n obstacles blocking your path. k-th obstacle is denoted by three integers ak, lk and rk, and it forbids entering any cell (ak, j) such that lk ≤ j ≤ rk.

You have to calculate the number of different paths from (2, 1) to (2, m), and print it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 104, 3 ≤ m ≤ 1018) — the number of obstacles and the number of columns in the matrix, respectively.

Then n lines follow, each containing three integers ak, lk and rk (1 ≤ ak ≤ 3, 2 ≤ lk ≤ rk ≤ m - 1) denoting an obstacle blocking every cell (ak, j) such that lk ≤ j ≤ rk. Some cells may be blocked by multiple obstacles.

Output

Print the number of different paths from (2, 1) to (2, m), taken modulo 109 + 7. If it is impossible to get from (2, 1) to (2, m), then the number of paths is 0.

Example

Input

2 5
1 3 4
2 2 3


Output

2
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using maxHeap = priority_queue<T, vector<T>, less<T>>;
template <typename T>
using minHeap = priority_queue<T, vector<T>, greater<T>>;
template <typename Iter>
ostream& _out(ostream& s, Iter b, Iter e) {
  s << "[";
  for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it;
  s << "]";
  return s;
}
template <typename A, typename B>
ostream& operator<<(ostream& s, const pair<A, B>& p) {
  return s << "(" << p.first << "," << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, const vector<T>& c) {
  return _out(s, begin(c), end(c));
}
bool debug = 0;
template <typename T>
void DEBUG(const T& x) {
  if (debug) cerr << x;
}
template <typename T, typename... Args>
void DEBUG(const T& head, const Args&... tail) {
  if (debug) {
    cerr << head;
    DEBUG(tail...);
  }
}
template <int _MOD>
struct ModInt {
  static const auto MOD = _MOD;
  template <class T>
  using integral_only = typename enable_if<is_integral<T>::value>::type;
  int x;
  constexpr ModInt() : x() {}
  template <class T, integral_only<T>* = nullptr>
  ModInt(T _x) {
    x = _x % MOD;
    if (x < 0) x += MOD;
  }
  ModInt operator-() const { return {x == 0 ? 0 : MOD - x}; }
  ModInt& operator+=(ModInt rhs) {
    x += rhs.x;
    if (x >= MOD) x -= MOD;
    return *this;
  }
  ModInt& operator-=(ModInt rhs) {
    x -= rhs.x;
    if (x < 0) x += MOD;
    return *this;
  }
  ModInt& operator*=(ModInt rhs) {
    x = (long long)x * rhs.x % MOD;
    return *this;
  }
  ModInt& operator/=(ModInt rhs) { return *this *= rhs.inv(); }
  ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
  ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
  ModInt inv() const {
    int a = x, b = MOD, u = 1, v = 0;
    while (b != 0) {
      int t = a / b;
      a -= t * b;
      u -= t * v;
      swap(a, b);
      swap(u, v);
    }
    return u;
  }
  template <class T, integral_only<T>* = nullptr>
  ModInt pow(T e) {
    ModInt r = 1, p = *this;
    while (e) {
      if (e & 1) r *= p;
      p *= p;
      e >>= 1;
    }
    return r;
  }
  bool operator==(ModInt rhs) const { return x == rhs.x; }
  bool operator!=(ModInt rhs) const { return x != rhs.x; }
  bool operator<(ModInt rhs) const { return x < rhs.x; }
  bool operator<=(ModInt rhs) const { return x <= rhs.x; }
  bool operator>(ModInt rhs) const { return x > rhs.x; }
  bool operator>=(ModInt rhs) const { return x >= rhs.x; }
  friend string to_string(ModInt i) { return to_string(i.x); }
  friend ostream& operator<<(ostream& os, ModInt i) { return os << i.x; }
};
const int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
struct Matrix {
  int n, m;
  vector<vector<mint>> vec;
  void build(int n_, int m_) {
    n = n_, m = m_;
    vec.resize(n, vector<mint>(m));
  }
  vector<mint>& operator[](int a) { return vec[a]; }
  Matrix operator*(const Matrix& a) {
    assert(m == a.n);
    Matrix ret;
    ret.build(n, a.m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < a.m; j++) {
        ret[i][j] = 0;
        for (int k = 0; k < m; k++) ret[i][j] += vec[i][k] * a.vec[k][j];
      }
    }
    return ret;
  }
  Matrix operator*=(const Matrix& a) {
    assert(m == a.n);
    Matrix ret;
    ret.build(n, a.m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < a.m; j++) {
        ret[i][j] = 0;
        for (int k = 0; k < m; k++) ret[i][j] += vec[i][k] * a.vec[k][j];
      }
    }
    swap(vec, ret.vec);
    return *this;
  }
  friend ostream& operator<<(ostream& ss, const Matrix& a) {
    ss << "[";
    for (int i = 0; i < a.n; i++)
      for (int j = 0; j < a.m; j++) {
        if (j == 0) ss << "[";
        ss << a.vec[i][j] << " ]"[j == a.m - 1];
      }
    ss << "]";
    return ss;
  }
};
map<int64_t, vector<pair<int, bool>>> mp;
map<int, int> cnt;
Matrix qPow(Matrix a, int64_t k) {
  assert(a.n == a.m);
  Matrix ret;
  ret.build(a.n, a.n);
  for (int i = 0; i < a.n; i++) ret[i][i] = 1;
  while (k) {
    if (k & 1) ret *= a;
    k >>= 1;
    a *= a;
  }
  return ret;
}
int main(int argc, char* argv[]) {
  if (argc > 1 and string(argv[1]) == "-D") debug = 1;
  if (!debug) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
  int64_t n, m;
  cin >> n >> m;
  mp[1] = vector<pair<int, bool>>();
  mp[m] = vector<pair<int, bool>>();
  for (int i = 0; i < n; i++) {
    int a;
    int64_t l, r;
    cin >> a >> l >> r;
    mp[l].emplace_back(a - 1, true);
    mp[r + 1].emplace_back(a - 1, false);
  }
  Matrix tran, dp;
  tran.build(3, 3);
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) tran[i][j] = 1;
  tran[0][2] = 0;
  tran[2][0] = 0;
  dp.build(1, 3);
  dp[0][1] = 1;
  auto ed = --mp.end();
  cnt[0] = cnt[1] = cnt[2] = 0;
  for (auto it = mp.begin(); it != ed; it++) {
    auto nxt = ++it;
    it--;
    for (auto a : it->second) {
      if (a.second)
        cnt[a.first]++;
      else
        cnt[a.first]--;
    }
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) {
        tran[i][j] = (cnt[i] == 0);
      }
    tran[0][2] = 0;
    tran[2][0] = 0;
    Matrix tmp = qPow(tran, nxt->first - it->first);
    dp *= tmp;
  }
  cout << dp[0][1] << '\n';
  return 0;
}
