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;
const long long MOD = (long long)1e9 + 7;
struct Matr {
  long long n;
  vector<vector<long long>> matr;
  Matr(long long n, long long k) : n(n), matr(n, vector<long long>(n, k)) {}
  Matr operator*(const Matr &b) const {
    Matr ans(n, 0);
    for (long long i = 0; i < n; ++i) {
      for (long long j = 0; j < n; ++j) {
        for (long long k = 0; k < n; ++k) {
          ans.matr[i][j] += matr[i][k] * b.matr[k][j] % MOD;
          ans.matr[i][j] %= MOD;
        }
      }
    }
    return ans;
  }
  Matr binPow(long long k) {
    if (k == 0) {
      Matr ans(3, 0);
      ans.matr[0][0] = 1;
      ans.matr[1][1] = 1;
      ans.matr[2][2] = 1;
      return ans;
    }
    if (k == 1) {
      return *this;
    }
    Matr res = binPow(k / 2);
    res = res * res;
    if (k % 2) {
      return res * *this;
    } else {
      return res;
    }
  }
};
struct Event {
  long long pos, typ, lvl;
  bool operator<(const Event &e) const {
    return pos < e.pos || pos == e.pos && typ < e.typ;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long m;
  cin >> m;
  Matr main(3, 1);
  main.matr[0][2] = 0;
  main.matr[2][0] = 0;
  Matr ans(3, 0);
  ans.matr[0][1] = 1;
  vector<Matr> trans(8, Matr(3, 1));
  for (long long mask = 0; mask < 7; ++mask) {
    trans[mask].matr[0][2] = 0;
    trans[mask].matr[2][0] = 0;
    for (long long i = 0; i < 3; ++i) {
      if (mask & (1 << i)) {
        for (long long j = 0; j < 3; ++j) {
          trans[mask].matr[j][i] = 0;
        }
      }
    }
  }
  vector<Event> data;
  for (long long i = 0; i < n; ++i) {
    long long a, l, r;
    cin >> a >> l >> r;
    data.push_back({l, 1, a - 1});
    data.push_back({r + 1, 0, a - 1});
  }
  sort(data.begin(), data.end());
  vector<pair<long long, long long>> segs;
  if (data[0].pos != 2) {
    segs.emplace_back(data[0].pos - 2, 0);
  }
  long long now = 1 << data[0].lvl;
  long long q[3];
  q[0] = 0;
  q[1] = 0;
  q[2] = 0;
  q[data[0].lvl]++;
  for (long long i = 1; i < data.size(); ++i) {
    if (data[i].pos - data[i - 1].pos != 0) {
      segs.emplace_back(data[i].pos - data[i - 1].pos, now);
    }
    long long ind = data[i].lvl;
    if (!data[i].typ) {
      q[ind]--;
      if (q[ind] == 0) {
        now -= 1 << ind;
      }
    } else {
      if (q[ind] == 0) {
        now += 1 << ind;
      }
      q[ind]++;
    }
    if (now == 7) {
      cout << 0 << "\n";
      return 0;
    }
  }
  if (data.back().pos != m) {
    segs.emplace_back(m - data.back().pos, 0);
  }
  for (auto &seg : segs) {
    ans = ans * trans[seg.second].binPow(seg.first);
  }
  cout << (ans.matr[0][0] + ans.matr[0][1] + ans.matr[0][2]) % MOD;
}
