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;
long long mod = 1e9 + 7;
struct mat {
  long long d[3][3];
  mat operator*(mat x) {
    mat res;
    res = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k < 3; k++) {
          res.d[i][j] += (d[i][k] * x.d[k][j]) % mod;
          res.d[i][j] %= mod;
        }
    return res;
  }
};
mat fpow(mat a, long long b) {
  mat res, t;
  res = {1, 0, 0, 0, 1, 0, 0, 0, 1};
  t = a;
  for (int i = 0; i < 3; i++) res.d[i][i] = 1;
  while (b) {
    if (b & 1) res = res * t;
    t = t * t;
    b >>= 1;
  }
  return res;
}
struct event {
  long long r, c, type;
};
bool cmp(event a, event b) { return a.c < b.c; }
long long N, M;
map<long long, vector<event>> mp;
int cnt[3];
long long cur[3];
long long ncur[3];
int main() {
  scanf("%lld%lld", &N, &M);
  vector<event> v;
  for (int i = 1; i <= N; i++) {
    long long a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    mp[b].push_back({a - 1, b, 1});
    mp[c + 1].push_back({a - 1, c + 1, -1});
  }
  vector<long long> xlist;
  for (auto e : mp) xlist.push_back(e.first);
  cur[0] = 0;
  cur[1] = 1;
  cur[0] = 0;
  long long x1 = 1;
  if (xlist[(int)xlist.size() - 1] != M) xlist.push_back(M);
  for (int i = 0; i < (int)xlist.size(); i++) {
    long long x2 = xlist[i] - 1;
    long long advance = x2 - x1;
    if (i != 0) {
      ncur[0] = (cur[0] + cur[1]) % mod;
      ncur[1] = (cur[0] + cur[1] + cur[2]) % mod;
      ncur[2] = (cur[1] + cur[2]) % mod;
      for (int i = 0; i < 3; i++) cur[i] = ncur[i];
    }
    if (cnt[0] == 0 && cnt[1] == 0 && cnt[2] == 0) {
      mat m = {1, 1, 0, 1, 1, 1, 0, 1, 1};
      m = fpow(m, advance);
      ncur[0] = (cur[0] * m.d[0][0] % mod + cur[1] * m.d[0][1] % mod +
                 cur[2] * m.d[0][2] % mod) %
                mod;
      ncur[1] = (cur[0] * m.d[1][0] % mod + cur[1] * m.d[1][1] % mod +
                 cur[2] * m.d[1][2] % mod) %
                mod;
      ncur[2] = (cur[0] * m.d[2][0] % mod + cur[1] * m.d[2][1] % mod +
                 cur[2] * m.d[2][2] % mod) %
                mod;
      for (int i = 0; i < 3; i++) cur[i] = ncur[i];
    } else if (cnt[0] == 0 && cnt[1] == 0) {
      cur[2] = 0;
      mat m = {1, 1, 0, 1, 1, 0, 0, 0, 0};
      m = fpow(m, advance);
      ncur[0] = (cur[0] * m.d[0][0] % mod + cur[1] * m.d[0][1] % mod +
                 cur[2] * m.d[0][2] % mod) %
                mod;
      ncur[1] = (cur[0] * m.d[1][0] % mod + cur[1] * m.d[1][1] % mod +
                 cur[2] * m.d[1][2] % mod) %
                mod;
      ncur[2] = (cur[0] * m.d[2][0] % mod + cur[1] * m.d[2][1] % mod +
                 cur[2] * m.d[2][2] % mod) %
                mod;
      for (int i = 0; i < 3; i++) cur[i] = ncur[i];
    } else if (cnt[0] == 0 && cnt[2] == 0) {
      cur[1] = 0;
    } else if (cnt[1] == 0 && cnt[2] == 0) {
      cur[0] = 0;
      mat m = {0, 0, 0, 0, 1, 1, 0, 1, 1};
      m = fpow(m, advance);
      ncur[0] = (cur[0] * m.d[0][0] % mod + cur[1] * m.d[0][1] % mod +
                 cur[2] * m.d[0][2] % mod) %
                mod;
      ncur[1] = (cur[0] * m.d[1][0] % mod + cur[1] * m.d[1][1] % mod +
                 cur[2] * m.d[1][2] % mod) %
                mod;
      ncur[2] = (cur[0] * m.d[2][0] % mod + cur[1] * m.d[2][1] % mod +
                 cur[2] * m.d[2][2] % mod) %
                mod;
      for (int i = 0; i < 3; i++) cur[i] = ncur[i];
    } else if (cnt[0] == 0) {
      cur[1] = cur[2] = 0;
    } else if (cnt[1] == 0) {
      cur[0] = cur[2] = 0;
    } else if (cnt[2] == 0) {
      cur[0] = cur[1] = 0;
    } else {
      printf("0\n");
      return 0;
    }
    x1 = xlist[i];
    for (auto e : mp[x1]) {
      cnt[e.r] += e.type;
    }
  }
  ncur[0] = (cur[0] + cur[1]) % mod;
  ncur[1] = (cur[0] + cur[1] + cur[2]) % mod;
  ncur[2] = (cur[1] + cur[2]) % mod;
  for (int i = 0; i < 3; i++) cur[i] = ncur[i];
  mat m = {1, 1, 0, 1, 1, 1, 0, 1, 1};
  m = fpow(m, M - xlist[(int)xlist.size() - 1]);
  ncur[0] = (cur[0] * m.d[0][0] % mod + cur[1] * m.d[0][1] % mod +
             cur[2] * m.d[0][2] % mod) %
            mod;
  ncur[1] = (cur[0] * m.d[1][0] % mod + cur[1] * m.d[1][1] % mod +
             cur[2] * m.d[1][2] % mod) %
            mod;
  ncur[2] = (cur[0] * m.d[2][0] % mod + cur[1] * m.d[2][1] % mod +
             cur[2] * m.d[2][2] % mod) %
            mod;
  for (int i = 0; i < 3; i++) cur[i] = ncur[i];
  printf("%lld\n", cur[1]);
}
