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;
using LL = long long;
const int Msize = 3;
const int M = 1000000007;
struct Matrix {
  int n, mat[Msize][Msize];
  void init(int _n, int a = 0) {
    n = _n;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) mat[i][j] = a;
  }
  void initOne(int _n) {
    n = _n;
    memset(mat, 0, sizeof(mat));
    for (int i = 0; i < n; i++) mat[i][i] = 1;
  }
  int inv(int x) {
    if (x < 2) return x;
    return (LL)M / x * (M - inv(M % x)) % M;
  }
  Matrix operator*(const Matrix &b) const {
    Matrix ret;
    ret.init(n);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          ret.mat[i][j] =
              (ret.mat[i][j] + ((LL)mat[i][k] * b.mat[k][j]) % M) % M;
        }
      }
    return ret;
  }
  void pput() {
    printf("sz = %d\n", n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) printf("%d ", mat[i][j]);
      cout << endl;
    }
  }
};
Matrix pow_M(Matrix a, LL n) {
  Matrix ret;
  ret.initOne(a.n);
  for (; n; n >>= 1, a = a * a)
    if (n & 1) ret = ret * a;
  return ret;
}
const int N = 1e4 + 10;
LL x[N * 2];
int xn;
int dp[3], tp[3], crr[3];
int a[N], st[N], ed[N];
LL L[N], R[N];
void add(int &u, int v) {
  u += v;
  if (u >= M) u -= M;
}
int main() {
  int n;
  LL m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> L[i] >> R[i];
    a[i]--;
    x[xn++] = L[i];
    x[xn++] = R[i] + 1;
    ed[i] = i;
    st[i] = i;
  }
  sort(ed, ed + n, [&](int i, int j) { return R[i] < R[j]; });
  sort(st, st + n, [&](int i, int j) { return L[i] < L[j]; });
  x[xn++] = 1;
  x[xn++] = m + 1;
  sort(x, x + xn);
  xn = unique(x, x + xn) - x;
  int edp = 0, stp = 0;
  for (int i = 0; i + 1 < xn; i++) {
    while (edp < n && R[ed[edp]] + 1 == x[i]) crr[a[ed[edp++]]]--;
    while (stp < n && L[st[stp]] == x[i]) crr[a[st[stp++]]]++;
    if (i == 0)
      dp[1] = 1;
    else {
      memcpy(tp, dp, sizeof dp);
      memset(dp, 0, sizeof dp);
      for (int i = 0; i < 3; i++)
        for (int j = -1; j <= 1; j++) {
          if (i + j >= 0 && i + j < 3 && crr[i + j] == 0) add(dp[i + j], tp[i]);
        }
    }
    Matrix y;
    y.init(3);
    for (int i = 0; i < 3; i++)
      if (!crr[i])
        for (int j = 0; j < 3; j++)
          if (!crr[j])
            if (abs(i - j) <= 1) y.mat[j][i] = 1;
    memset(tp, 0, sizeof tp);
    y = pow_M(y, x[i + 1] - x[i] - 1);
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        add(tp[i], 1ll * dp[j] * y.mat[i][j] % M);
      }
    }
    memcpy(dp, tp, sizeof dp);
  }
  cout << dp[1] << endl;
  return 0;
}
