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>
class Task {
  static constexpr size_t MOD = 1e9 + 7;
  static constexpr size_t MAX_N = 10000 + 4;
  class Mat {
    static constexpr size_t N = 3;
    int64_t data_[N][N];

   public:
    Mat() { Clear(0); }
    template <typename Int>
    Mat(Int data[N][N]) {
      for (size_t i = 0; i < N; i += 1)
        for (size_t j = 0; j < N; j += 1) data_[i][j] = data[i][j];
    }
    int64_t &operator()(size_t a, size_t b) { return data_[a][b]; }
    int64_t operator()(size_t a, size_t b) const { return data_[a][b]; }
    size_t size() { return N; }
    void Clear(int flag) {
      for (size_t i = 0; i < N; i += 1) {
        for (size_t j = 0; j < N; j += 1) {
          if (i == j)
            data_[i][j] = flag;
          else
            data_[i][j] = 0;
        }
      }
    }
  };
  void MatX(Mat const &m1, Mat const &m2, Mat &m) {
    m.Clear(0);
    for (size_t i = 0; i < m.size(); i += 1) {
      for (size_t j = 0; j < m.size(); j += 1) {
        if (m1(i, j) == 0) continue;
        for (size_t k = 0; k < m.size(); k += 1) m(i, k) += m1(i, j) * m2(j, k);
      }
    }
    for (size_t i = 0; i < m.size(); i += 1)
      for (size_t j = 0; j < m.size(); j += 1) m(i, j) %= MOD;
  }
  void MatX(Mat &m1, Mat const &m2) {
    static Mat tmp;
    MatX(m1, m2, tmp);
    m1 = tmp;
  }
  void MatP(Mat m1, int64_t p, Mat &m) {
    m.Clear(1);
    for (; p; p >>= 1) {
      if (p & 1) MatX(m, m1);
      MatX(m1, m1);
    }
  }
  void Transform(Mat const &t, int64_t &a, int64_t &b, int64_t &c) {
    int64_t aa = a * t(0, 0) + b * t(0, 1) + c * t(0, 2);
    int64_t bb = a * t(1, 0) + b * t(1, 1) + c * t(1, 2);
    int64_t cc = a * t(2, 0) + b * t(2, 1) + c * t(2, 2);
    a = aa % MOD;
    b = bb % MOD;
    c = cc % MOD;
  }

 public:
  void Run() {
    size_t n;
    int64_t m;
    std::cin >> n >> m;
    static struct KeyPoint {
      int row;
      int64_t col;
      int flag;
      struct ColOrder {
        bool operator()(KeyPoint const k1, KeyPoint const &k2) {
          return k1.col < k2.col;
        }
      };
    } kp[MAX_N * 2];
    for (size_t i = 0; i < n; ++i) {
      int row;
      int64_t l, r;
      std::cin >> row >> l >> r;
      row -= 1;
      l -= 1;
      kp[i << 1] = {row, l, 1};
      kp[i << 1 | 1] = {row, r, -1};
    }
    kp[n << 1] = {0, m, 0};
    size_t nn = n + n + 1;
    std::sort(kp, kp + nn, KeyPoint::ColOrder());
    int64_t a = 0;
    int64_t b = 1;
    int64_t c = 0;
    int64_t prev = 1;
    int sum_flag[3] = {0};
    for (size_t i = 0; i < nn; ++i) {
      int64_t curr = kp[i].col;
      if (prev == curr) {
        sum_flag[kp[i].row] += kp[i].flag;
        continue;
      }
      int s0 = !sum_flag[0];
      int s1 = !sum_flag[1];
      int s2 = !sum_flag[2];
      int tmp_transform[3][3] = {s0, s0, 0, s1, s1, s1, 0, s2, s2};
      Mat transform(tmp_transform);
      static Mat transform_all;
      MatP(transform, curr - prev, transform_all);
      Transform(transform_all, a, b, c);
      prev = curr;
      sum_flag[kp[i].row] += kp[i].flag;
    }
    std::cout << b << std::endl;
  }
};
int main() {
  auto task = std::make_shared<Task>();
  task->Run();
  return 0;
}
