#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;

    Matrix res;
    res.a[0][0] = 1;

    m = 16;

    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {
        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        for(int i=c+1; i<m; i++)
        {
            res.a[i][0] = 0;
        }
        Fill(mul.a,0);
        for(int i=0; i<=c; i++)
        {
            for(int j=max(0,i-1); j<=min(c,i+1); j++) mul.a[i][j] = 1;
        }

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        res = res*then;
        // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //cout<<endl;
    }

    cout<<res.a[0][0];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

void Init()
{

    for(int i=0; i<m; i++)
    {
        mul.a[i][i] = 1;

        if((i-1) >= 0) mul.a[i][i-1] = 1;

        if((i+1) < m) mul.a[i][i+1] = 1;
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;
    lli ans = 1;

    vector<lli>last(20,0);
    last[15] = 1;

    m = 16;
    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {
        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        for(int i=0; i<(m-c-1); i++)last[i] = 0;
        if(c==0)continue;
        Init();
        vector<lli>now(20,0);

        for(int i=0; i<m-c-1; i++){
            for(int j=0; j<m; j++) mul.a[i][j] = 0;
        }

        if((m-c-2)>=0) mul.a[m-c-1][m-c-2] = 0;

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        for(int i=m-c-1; i<m; i++)
        {

            now[i] = (then.a[i][i]*last[i])%mod;

            if((i+1)<m) now[i] = (now[i] + (then.a[i][i+1]*last[i+1])%mod ) % mod;

            if((i-1)>=(m-c-1)) now[i] = (now[i] + (then.a[i][i-1]*last[i-1])%mod ) % mod;
        }
        last = now;
        // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //cout<<endl;
    }

    cout<<last[15];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

void Init()
{

    for(int i=0; i<m; i++)
    {
        mul.a[i][i] = 1;

        if((i-1) >= 0) mul.a[i][i-1] = 1;

        if((i+1) < m) mul.a[i][i+1] = 1;
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;
    lli ans = 1;

    vector<lli>last(20,0);
    last[15] = 1;

    m = 16;
    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {
        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        for(int i=0; i<(m-c-1); i++)last[i] = 0;
        if(c==0)continue;
        Init();
        vector<lli>now(20,0);

        for(int i=0; i<m-c-1; i++){
            for(int j=0; j<m; j++) mul.a[i][j] = 0;
        }

        //if((m-c-2)>=0) mul.a[m-c-1][m-c-2] = 0;

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        for(int i=m-c-1; i<m; i++)
        {

            now[i] = (then.a[i][i]*last[i])%mod;

            if((i+1)<m) now[i] = (now[i] + (then.a[i][i+1]*last[i+1])%mod ) % mod;

            if((i-1)>=(m-c-1)) now[i] = (now[i] + (then.a[i][i-1]*last[i-1])%mod ) % mod;
        }
        last = now;
        // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //cout<<endl;
    }

    cout<<last[15];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

void Init(){

    for(int i=0; i<m; i++)
    {
        mul.a[i][i] = 1;

        if((i-1) >= 0) mul.a[i][i-1] = 1;

        if((i+1) < m) mul.a[i][i+1] = 1;
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;
    lli ans = 1;

    vector<lli>last(20,0);
    last[15] = 1;

    m = 16;
    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {
        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        if(c==0){
            for(int i=0; i<15; i++)last[i] = 0;
            continue;
        }

        vector<lli>now(20,0);

        Init();

        for(int i=0; i<(m-c-1); i++){
            for(int j=0; j<m; j++) mul.a[i][j] = 0;
        }

        if((m-c-2)>=0) mul.a[m-c-1][m-c-2] = 0;

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        for(int i=m-c-1; i<m; i++){

            now[i] = (then.a[i][i]*last[i])%mod;

            if((i+1)<m) now[i] = (now[i] + (then.a[i][i+1]*last[i+1])%mod ) % mod;

            if((i-1)>=(m-c-1)) now[i] = (now[i] + (then.a[i][i-1]*last[i-1])%mod ) % mod;
        }
        last = now;
      // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //cout<<endl;

        if(b==k)break;
    }

    cout<<last[15];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

void Init()
{

    for(int i=0; i<m; i++)
    {
        mul.a[i][i] = 1;

        if((i-1) >= 0) mul.a[i][i-1] = 1;

        if((i+1) < m) mul.a[i][i+1] = 1;
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;
    lli ans = 1;

    vector<lli>last(20,0);
    last[15] = 1;

    m = 16;
    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {
        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        for(int i=0; i<(m-c-1); i++)last[i] = 0;

        Init();
        vector<lli>now(20,0);

        for(int i=0; i<m-c-1; i++){
            for(int j=0; j<m; j++) mul.a[i][j] = 0;
        }

        if((m-c-2)>=0) mul.a[m-c-1][m-c-2] = 0;

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        for(int i=m-c-1; i<m; i++)
        {

            now[i] = (then.a[i][i]*last[i])%mod;

            if((i+1)<m) now[i] = (now[i] + (then.a[i][i+1]*last[i+1])%mod ) % mod;

            if((i-1)>=(m-c-1)) now[i] = (now[i] + (then.a[i][i-1]*last[i-1])%mod ) % mod;
        }
        last = now;
        // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //cout<<endl;
    }

    cout<<last[15];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

void Init(){

    for(int i=0; i<m; i++)
    {
        mul.a[i][i] = 1;

        if((i-1) >= 0) mul.a[i][i-1] = 1;

        if((i+1) < m) mul.a[i][i+1] = 1;
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;
    lli ans = 1;

    vector<lli>last(20,0);
    last[15] = 1;

    m = 16;
    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {

        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        vector<lli>now(20,0);

        Init();

        for(int i=0; i<m-c-1; i++){
            for(int j=0; j<m; j++) mul.a[i][j] = 0;
        }

        if((m-c-2)>=0) mul.a[m-c-1][m-c-2] = 0;

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        for(int i=m-c-1; i<m; i++){

            now[i] = (then.a[i][i]*last[i])%mod;

            if((i+1)<=(m-c-1)) now[i] = (now[i] + (then.a[i][i+1]*last[i+1])%mod ) % mod;

            if((i-1)>=0) now[i] = (now[i] + (then.a[i][i-1]*last[i-1])%mod ) % mod;
        }
        last = now;
       // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //last = now;

        //cout<<endl;
    }

    cout<<last[15];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

void Init(){

    for(int i=0; i<m; i++)
    {
        mul.a[i][i] = 1;

        if((i-1) >= 0) mul.a[i][i-1] = 1;

        if((i+1) < m) mul.a[i][i+1] = 1;
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;
    lli ans = 1;

    vector<lli>last(20,0);
    last[15] = 1;

    m = 16;
    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {
        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        if(c==0){
            for(int i=0; i<(m-1); i++)last[i] = 0;
            continue;
        }

        vector<lli>now(20,0);

        Init();

        for(int i=0; i<(m-c-1); i++){
            for(int j=0; j<m; j++) mul.a[i][j] = 0;
        }

        if((m-c-2)>=0) mul.a[m-c-1][m-c-2] = 0;

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        for(int i=0; i<(m-c-1);i++)last[i] = 0;

        for(int i=m-c-1; i<m; i++){

            now[i] = (then.a[i][i]*last[i])%mod;

            if((i+1)<m) now[i] = (now[i] + (then.a[i][i+1]*last[i+1])%mod ) % mod;

            if((i-1)>=(m-c-1)) now[i] = (now[i] + (then.a[i][i-1]*last[i-1])%mod ) % mod;
        }
        last = now;
      // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //cout<<endl;
    }

    cout<<last[15];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

#define M 20

int m;

struct Matrix
{
    int a[M][M];

    Matrix()
    {
        Fill(a,0);
    }

    Matrix operator*(const Matrix &b)
    {
        Matrix ret;

        for(int i=0; i<m; i++)
        {

            for(int j=0; j<m; j++)
            {

                lli sum = 0;

                for(int k=0; k<m; k++)
                {

                    sum = (sum + (a[i][k]*1LL*b.a[k][j])) % mod;
                }

                ret.a[i][j] = sum;
            }
        }
        return ret;
    }
} mul,I;

Matrix Mpow(lli n)
{

    Matrix ret = I; /// identity matrix

    while(n)
    {

        if(n%2) ret = ret*mul;

        n >>= 1;
        mul = mul*mul;
    }

    return ret;
}

void Init()
{

    for(int i=0; i<m; i++)
    {
        mul.a[i][i] = 1;

        if((i-1) >= 0) mul.a[i][i-1] = 1;

        if((i+1) < m) mul.a[i][i+1] = 1;
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;
    int n;
    lli k;
    cin>>n>>k;
    lli ans = 1;

    vector<lli>last(20,0);
    last[15] = 1;

    m = 16;
    for(int i=0; i<m; i++) I.a[i][i] = 1;

    while(n--)
    {
        lli a,b;
        int c;
        scn2ll(a,b);
        scn(c);

        if(c==0)
        {
            for(int i=0; i<m-1; i++)last[i] = 0;

            continue;
        }
        Init();
        vector<lli>now(20,0);

        Matrix then;
        b = min(b,k);
        then = Mpow(b-a);

        for(int i=m-c-1; i<m; i++)
        {

            now[i] = (then.a[i][i]*last[i])%mod;

            if((i+1)<m) now[i] = (now[i] + (then.a[i][i+1]*last[i+1])%mod ) % mod;

            if((i-1)>=(m-c-1)) now[i] = (now[i] + (then.a[i][i-1]*last[i-1])%mod ) % mod;
        }
        last = now;
        // for(int i=0; i<m; i++)cout<<last[i]<<" -- ";
        //cout<<endl;
    }

    cout<<last[15];

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


#include <iostream>
using namespace std;
long long int F[16][16],M[16][16],curr[16][16]={0};
const long long int MOD=1e9+7;
void multiply(long long int B[16][16],long long int A[16][16]){
    long long int mul[16][16];
    int i,j,k;
    for(i=0;i<16;i++) for(j=0;j<16;j++){
        mul[i][j]=0;
        for(k=0;k<16;k++) mul[i][j]=(mul[i][j]+((A[i][k]%MOD)*(B[k][j]%MOD))%MOD)%MOD;
    }
    for(i=0;i<16;i++) for(j=0;j<16;j++) B[i][j]=mul[i][j];
}
void power(long long int N){
    if(N==1) return;
    power(N/2);
    multiply(M,M);
    if(N&1) multiply(M,F);
}
int main(){
    int i,j,n,c;
    long long int a,b;
    long long int k;
    for(i=0;i<16;i++) curr[i][i]=1;
    cin >> n >> k;
    while(n--){
        cin >> a >> b >> c;
        for(i=0;i<16;i++) for(j=0;j<16;j++) F[i][j]=0;
        for(i=0;i<=c;i++){
            if(i<15) F[i][i+1]=1;
            if(i>0) F[i][i-1]=1;
            F[i][i]=1;
        }
        for(i=0;i<16;i++) for(j=0;j<16;j++) M[i][j]=F[i][j];
        if(min(b,k)>a){
            power(min(b,k)-a);
            multiply(curr,M);
        }
    }
    cout << curr[0][0];
}


#include <iostream>
using namespace std;
long long int F[16][16],M[16][16],curr[16][16]={0};
const int MOD=1e9+7;
void multiply(long long int B[16][16],long long int A[16][16]){
    long long int mul[16][16];
    int i,j,k;
    for(i=0;i<16;i++) for(j=0;j<16;j++){
        mul[i][j]=0;
        for(k=0;k<16;k++) mul[i][j]=(mul[i][j]+A[i][k]*B[k][j])%MOD;
    }
    for(i=0;i<16;i++) for(j=0;j<16;j++) B[i][j]=mul[i][j];
}
void power(long long int N){
    if(N==1) return;
    power(N/2);
    multiply(M,M);
    if(N&1) multiply(M,F);
}
int main(){
    int i,j,n,c;
    long long int a,b,ans=0;
    long long int k;
    for(i=0;i<16;i++) curr[i][i]=1;
    cin >> n >> k;
    while(n--){
        cin >> a >> b >> c;
        for(i=0;i<16;i++) for(j=0;j<16;j++) F[i][j]=0;
        for(i=0;i<=c;i++){
            if(i<15) F[i][i+1]=1;
            if(i>0) F[i][i-1]=1;
            F[i][i]=1;
        }
        for(i=0;i<16;i++) for(j=0;j<16;j++) M[i][j]=F[i][j];
        if(min(b,k)>a){
            power(min(b,k)-a);
            multiply(curr,M);
        }
    }
    cout << curr[0][0];
}


#include <iostream>
using namespace std;
long long int F[17][17],M[17][17],curr[17][17]={0};
const long long int MOD=1e9+7;
void multiply(long long int B[17][17],long long int A[17][17]){
    long long int mul[17][17];
    int i,j,k;
    for(i=0;i<17;i++) for(j=0;j<17;j++){
        mul[i][j]=0;
        for(k=0;k<17;k++) mul[i][j]=(mul[i][j]+((A[i][k]%MOD)*(B[k][j]%MOD))%MOD)%MOD;
    }
    for(i=0;i<17;i++) for(j=0;j<17;j++) B[i][j]=mul[i][j];
}
void power(long long int N){
    if(N==1) return;
    power(N/2);
    multiply(M,M);
    if(N%2==1) multiply(M,F);
}
int main(){
    int i,j,n,c;
    long long int a,b,k;
    for(i=0;i<17;i++) curr[i][i]=1;
    cin >> n >> k;
    while(n--){
        cin >> a >> b >> c;
        for(i=0;i<17;i++) for(j=0;j<17;j++) F[i][j]=0;
        for(i=0;i<=c;i++){
            if(i<c) F[i][i+1]=1;
            if(i>0) F[i][i-1]=1;
            F[i][i]=1;
        }
        for(i=0;i<17;i++) for(j=0;j<17;j++) M[i][j]=F[i][j];
        if(min(b,k)>a){
            power(min(b,k)-a);
            multiply(curr,M);
        }
    }
    cout << curr[0][0];
}


#include <iostream>
using namespace std;
long long int F[16][16],M[16][16],curr[16][16]={0};
const int MOD=1e9+7;
void multiply(long long int B[16][16],long long int A[16][16]){
    long long int mul[16][16];
    int i,j,k;
    for(i=0;i<16;i++) for(j=0;j<16;j++){
        mul[i][j]=0;
        for(k=0;k<16;k++) mul[i][j]=(mul[i][j]+((A[i][k]%MOD)*(B[k][j]%MOD))%MOD)%MOD;
    }
    for(i=0;i<16;i++) for(j=0;j<16;j++) B[i][j]=mul[i][j];
}
void power(long long int N){
    if(N==1) return;
    power(N/2);
    multiply(M,M);
    if(N&1) multiply(M,F);
}
int main(){
    int i,j,n,c;
    long long int a,b;
    long long int k;
    for(i=0;i<16;i++) curr[i][i]=1;
    cin >> n >> k;
    while(n--){
        cin >> a >> b >> c;
        for(i=0;i<16;i++) for(j=0;j<16;j++) F[i][j]=0;
        for(i=0;i<=c;i++){
            if(i<15) F[i][i+1]=1;
            if(i>0) F[i][i-1]=1;
            F[i][i]=1;
        }
        for(i=0;i<16;i++) for(j=0;j<16;j++) M[i][j]=F[i][j];
        if(min(b,k)>a){
            power(min(b,k)-a);
            multiply(curr,M);
        }
    }
    cout << curr[0][0];
}


#include <iostream>
using namespace std;
long long int F[16][16],M[16][16],curr[16][16]={0};
const int MOD=1e9+7;
void multiply(long long int B[16][16],long long int A[16][16]){
    long long int mul[16][16];
    int i,j,k;
    for(i=0;i<16;i++) for(j=0;j<16;j++){
        mul[i][j]=0;
        for(k=0;k<16;k++) mul[i][j]=(mul[i][j]+A[i][k]*B[k][j])%MOD;
    }
    for(i=0;i<16;i++) for(j=0;j<16;j++) B[i][j]=mul[i][j];
}
void power(long long int N){
    if(N==1) return;
    power(N/2);
    multiply(M,M);
    if(N&1) multiply(M,F);
}
int main(){
    int i,j,n,c;
    long long int a,b,ans=0;
    long long int k;
    for(i=0;i<16;i++) curr[i][i]=1;
    cin >> n >> k;
    while(n--){
        cin >> a >> b >> c;
        for(i=0;i<16;i++) for(j=0;j<16;j++) F[i][j]=0;
        for(i=0;i<=c;i++){
            if(i<15) F[i][i+1]=1;
            if(i>0) F[i][i-1]=1;
            F[i][i]=1;
        }
        for(i=0;i<16;i++) for(j=0;j<16;j++) M[i][j]=F[i][j];
        power(min(b,k)-a);
        multiply(curr,M);
    }
    cout << curr[0][0];
}


#include <iostream>
using namespace std;
long long int F[16][16],M[16][16],curr[16][16]={0};
const int MOD=1e9+7;
void multiply(long long int B[16][16],long long int A[16][16]){
    long long int mul[16][16];
    int i,j,k;
    for(i=0;i<16;i++) for(j=0;j<16;j++){
        mul[i][j]=0;
        for(k=0;k<16;k++) mul[i][j]=(mul[i][j]+(A[i][k]%MOD)*(B[k][j]%MOD))%MOD;
    }
    for(i=0;i<16;i++) for(j=0;j<16;j++) B[i][j]=mul[i][j];
}
void power(long long int N){
    if(N==1) return;
    power(N/2);
    multiply(M,M);
    if(N&1) multiply(M,F);
}
int main(){
    int i,j,n,c;
    long long int a,b,ans=0;
    long long int k;
    for(i=0;i<16;i++) curr[i][i]=1;
    cin >> n >> k;
    while(n--){
        cin >> a >> b >> c;
        for(i=0;i<16;i++) for(j=0;j<16;j++) F[i][j]=0;
        for(i=0;i<=c;i++){
            if(i<15) F[i][i+1]=1;
            if(i>0) F[i][i-1]=1;
            F[i][i]=1;
        }
        for(i=0;i<16;i++) for(j=0;j<16;j++) M[i][j]=F[i][j];
        if(min(b,k)>a){
            power(min(b,k)-a);
            multiply(curr,M);
        }
    }
    cout << curr[0][0];
}


#include <iostream>
using namespace std;
long long int F[17][17],M[17][17],curr[17][17]={0};
const long long int MOD=1e9+7;
void multiply(long long int B[17][17],long long int A[17][17]){
    long long int mul[17][17];
    int i,j,k;
    for(i=0;i<17;i++) for(j=0;j<17;j++){
        mul[i][j]=0;
        for(k=0;k<17;k++) mul[i][j]=(mul[i][j]+((A[i][k]%MOD)*(B[k][j]%MOD))%MOD)%MOD;
    }
    for(i=0;i<17;i++) for(j=0;j<17;j++) B[i][j]=mul[i][j];
}
void power(long long int N){
    if(N==1) return;
    power(N/2);
    multiply(M,M);
    if(N&((long long int)1)) multiply(M,F);
}
int main(){
    int i,j,n,c;
    long long int a,b,k;
    for(i=0;i<17;i++) curr[i][i]=1;
    cin >> n >> k;
    while(n--){
        cin >> a >> b >> c;
        for(i=0;i<17;i++) for(j=0;j<17;j++) F[i][j]=0;
        for(i=0;i<=c;i++){
            if(i<16) F[i][i+1]=1;
            if(i>0) F[i][i-1]=1;
            F[i][i]=1;
        }
        for(i=0;i<17;i++) for(j=0;j<17;j++) M[i][j]=F[i][j];
        if(min(b,k)>a){
            power(min(b,k)-a);
            multiply(curr,M);
        }
    }
    cout << curr[0][0];
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & -(x))
#define BIT(a,b) ((ll)((a)&(1<<(b)))==0 ? false : true)

const ll MAXN=20;
const ll Mod=1e9+7;

ll b[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll matrix[MAXN][MAXN];
ll matrix_m[MAXN][MAXN];

void mult_to_matrix()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult_to_b()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*b[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix_m[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix_m[i][j]=temp[i][j];
        }
    }
}

void matrix_pw(ll k)
{
    if (k==1)
    {
        return;
    }

    matrix_pw(k/2);
    mult_to_matrix();

    if (k&1)
    {
        mult_to_b();
    }
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);

    ll n,k;
    cin>>n>>k;

    for(ll i=0;i<MAXN;i++)
    {
        matrix_m[i][i]=1;
    }

    for(ll i=0;i<n;i++)
    {
        ll x,y,c;
        cin>>x>>y>>c;

        y=min(y , k);

        for(ll j=0;j<MAXN;j++)
        {
            for(ll u=0;u<MAXN;u++)
            {
                matrix[j][u]=0;
            }
        }

        for(ll j=0;j<=c;j++)
        {
            matrix[j][min(j+1 , c)]=1;
            matrix[j][j]=1;
            matrix[j][max(j-1 , (ll)(0))]=1;
        }

        for(ll j=0;j<MAXN;j++)
        {
            for(ll u=0;u<MAXN;u++)
            {
                b[j][u]=matrix[j][u];
            }
        }

        matrix_pw(y-x);

        mult();
    }

    cout<<matrix_m[0][0];
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & -(x))
#define BIT(a,b) ((ll)((a)&(1<<(b)))==0 ? false : true)

const ll MAXN=17;
const ll Mod=1e9+7;

ll b[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll matrix[MAXN][MAXN];
ll matrix_m[MAXN][MAXN];

void mult_to_matrix()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult_to_b()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*b[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*matrix_m[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix_m[i][j]=temp[i][j];
        }
    }
}

void matrix_pw(ll k)
{
    if (k==1)
    {
        return;
    }

    matrix_pw(k/2);
    mult_to_matrix();

    if (k&1)
    {
        mult_to_b();
    }
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);

    ll n,k;
    cin>>n>>k;

    for(ll i=0;i<n;i++)
    {
        ll x,y,c;
        cin>>x>>y>>c;

        y=min(y , k);

        for(ll j=0;j<=c;j++)
        {
            matrix[j][min(j+1 , c)]=1;
            matrix[j][j]=1;
            matrix[j][max(j-1 , (ll)(0))]=1;

            b[j][min(j+1 , c)]=1;
            b[j][j]=1;
            b[j][max(j-1 , (ll)(0))]=1;
        }

        matrix_pw(y-x);

        if (i==0)
        {
            for(ll i=0;i<=c;i++)
            {
                for(ll j=0;j<=c;j++)
                {
                    matrix_m[i][j]=matrix[i][j];
                }
            }
        }
        else
        {
            mult();
        }
    }

    cout<<matrix_m[0][0];
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & -(x))
#define BIT(a,b) ((ll)((a)&(1<<(b)))==0 ? false : true)

const ll MAXN=20;
const ll Mod=1e9+7;

ll b[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll matrix[MAXN][MAXN];
ll matrix_m[MAXN][MAXN];

void mult_to_matrix()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult_to_b()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*b[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix_m[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix_m[i][j]=temp[i][j];
        }
    }
}

void matrix_pw(ll k)
{
    if (k==1)
    {
        return;
    }

    matrix_pw(k/2);
    mult_to_matrix();

    if (k&1)
    {
        mult_to_b();
    }
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);

    ll n,k;
    cin>>n>>k;

    for(ll i=0;i<MAXN;i++)
    {
        matrix_m[i][i]=1;
    }

    for(ll i=0;i<n;i++)
    {
        ll x,y,c;
        cin>>x>>y>>c;

        y=min(y , k);

        for(ll j=0;j<MAXN;j++)
        {
            for(ll u=0;u<MAXN;u++)
            {
                matrix[j][u]=0;
            }
        }

        for(ll j=0;j<=c;j++)
        {
            matrix[j][min(j+1 , c)]=1;
            matrix[j][j]=1;
            matrix[j][max(j-1 , (ll)(0))]=1;
        }

        for(ll j=0;j<MAXN;j++)
        {
            for(ll u=0;u<MAXN;u++)
            {
                b[j][u]=matrix[j][u];
            }
        }

        if (x!=y)
        {
            matrix_pw(y-x);
            mult();
        }
    }

    cout<<matrix_m[0][0];
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & -(x))
#define BIT(a,b) ((ll)((a)&(1<<(b)))==0 ? false : true)

const ll MAXN=17;
const ll MAXW=103;
const ll Mod=1e9+7;

ll matrix[MAXN][MAXN][MAXW];

ll b[MAXN][MAXN];
ll temp[MAXN][MAXN];

void mult_to_matrix(ll id)
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u][id]*matrix[u][j][id];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j][id]=temp[i][j];
        }
    }
}

void mult_to_b(ll id)
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u][id]*b[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j][id]=temp[i][j];
        }
    }
}

void mult(ll id , ll id2)
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u][id]*matrix[u][j][id2];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j][id]=temp[i][j];
        }
    }
}

void matrix_pw(ll id , ll k)
{
    if (k==1)
    {
        return;
    }

    matrix_pw(id , k/2);
    mult_to_matrix(id);

    if (k&1)
    {
        mult_to_b(id);
    }
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);

    ll n,k;
    cin>>n>>k;

    for(ll i=0;i<n;i++)
    {
        ll x,y,c;
        cin>>x>>y>>c;

        y=min(y , k);

        for(ll j=0;j<=c;j++)
        {
            matrix[j][min(j+1 , c)][i]=1;
            matrix[j][j][i]=1;
            matrix[j][max(j-1 , (ll)(0))][i]=1;

            b[j][min(j+1 , c)]=1;
            b[j][j]=1;
            b[j][max(j-1 , (ll)(0))]=1;
        }

        matrix_pw(i , y-x);
    }

    for(ll i=1;i<n;i++)
    {
        mult(0 , i);
    }

    cout<<matrix[0][0][0];
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & -(x))
#define BIT(a,b) ((ll)((a)&(1<<(b)))==0 ? false : true)

const ll MAXN=20;
const ll Mod=1e9+7;

ll b[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll matrix[MAXN][MAXN];
ll matrix_m[MAXN][MAXN];

void mult_to_matrix()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult_to_b()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*b[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix_m[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix_m[i][j]=temp[i][j];
        }
    }
}

void matrix_pw(ll k)
{
    if (k==1)
    {
        return;
    }

    matrix_pw(k/2);
    mult_to_matrix();

    if (k&1)
    {
        mult_to_b();
    }
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);

    ll n,k;
    cin>>n>>k;

    for(ll i=0;i<MAXN;i++)
    {
        matrix_m[i][i]=1;
    }

    for(ll i=0;i<n;i++)
    {
        ll x,y,c;
        cin>>x>>y>>c;

        y=min(y , k);

        for(ll j=0;j<=c;j++)
        {
            matrix[j][min(j+1 , c)]=1;
            matrix[j][j]=1;
            matrix[j][max(j-1 , (ll)(0))]=1;
        }

        for(ll j=0;j<MAXN;j++)
        {
            for(ll u=0;u<MAXN;u++)
            {
                b[j][u]=matrix[j][u];
            }
        }

        matrix_pw(y-x);

        mult();
    }

    cout<<matrix_m[0][0];
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & -(x))
#define BIT(a,b) ((ll)((a)&(1<<(b)))==0 ? false : true)

const ll MAXN=17;
const ll Mod=1e9+7;

ll b[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll matrix[MAXN][MAXN];
ll matrix_m[MAXN][MAXN];

void mult_to_matrix()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*matrix[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult_to_b()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*b[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix[i][j]=temp[i][j];
        }
    }
}

void mult()
{
    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            temp[i][j]=0;

            for(ll u=0;u<MAXN;u++)
            {
                temp[i][j]+=matrix[i][u]*matrix_m[u][j];
                temp[i][j]%=Mod;
            }
        }
    }

    for(ll i=0;i<MAXN;i++)
    {
        for(ll j=0;j<MAXN;j++)
        {
            matrix_m[i][j]=temp[i][j];
        }
    }
}

void matrix_pw(ll k)
{
    if (k==1)
    {
        return;
    }

    matrix_pw(k/2);
    mult_to_matrix();

    if (k&1)
    {
        mult_to_b();
    }
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);

    ll n,k;
    cin>>n>>k;

    for(ll i=0;i<n;i++)
    {
        ll x,y,c;
        cin>>x>>y>>c;

        y=min(y , k);

        for(ll j=0;j<=c;j++)
        {
            matrix[j][min(j+1 , c)]=1;
            matrix[j][j]=1;
            matrix[j][max(j-1 , (ll)(0))]=1;

            b[j][min(j+1 , c)]=1;
            b[j][j]=1;
            b[j][max(j-1 , (ll)(0))]=1;
        }

        matrix_pw(y-x);

        if (i==0)
        {
            for(ll u=0;u<=c;u++)
            {
                for(ll w=0;w<=c;w++)
                {
                    matrix_m[u][w]=matrix[u][w];
                }
            }
        }
        else
        {
            mult();
        }
    }

    cout<<matrix_m[0][0];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

typedef struct
{
    ll mat[20][20];
}matrix;

matrix matrix_mul(matrix a,matrix b,ll y)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(ll i=0;i<=y;i++)
    {
        for(ll j=0;j<=y;j++)
        {
            for(ll k=0;k<=y;k++)
            {
                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
            }
        }
    }
    return c;
}

matrix matrix_quick_power(matrix a,ll k ,ll y)
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(int i=0;i<=15;i++)
        b.mat[i][i]=1;
    while(k)
    {
        if(k%2==1)  b=matrix_mul(b,a,y);
        k=k/2;
        a=matrix_mul(a,a,y);
    }
    return b;

}

matrix a,b,c;
int main(void)
{
    ll n,k;
    cin >> n >>k;
    memset(a.mat,0,sizeof(a.mat));
    for(int i=0;i<=15;i++)
        for(int j=i-1;j<=i-1+2;j++)
            if(j>=0 && j<=15)
                a.mat[i][j]=1;
    /*for(int i=0;i<=15;i++)
    {
        for(int j=0;j<=15;j++)
            printf("%d ",a.mat[i][j]);
        puts("");
    }*/
    c.mat[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        ll  l,r,y;
        scanf("%I64d%I64d%I64d",&l,&r,&y);
        if(r>k)  r=k;
        for(int i=y+1;i<=18;i++)    c.mat[i][0]=0;
        b=matrix_quick_power(a,r-l,y);
        c=matrix_mul(b,c,y);
    }
    printf("%I64d\n",c.mat[0][0]);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll MOD=1e9+7;
typedef struct
{
    ll mat[20][20];
}matrix;

matrix matrix_mul(matrix a,matrix b,ll y)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(ll i=0;i<=y;i++)
    {
        for(ll j=0;j<=y;j++)
        {
            for(ll k=0;k<=y;k++)
            {
                c.mat[i][j]+=(a.mat[i][k]%MOD*b.mat[k][j]%MOD)%MOD;
                c.mat[i][j]%=MOD;
            }
        }
    }
    return c;
}

matrix matrix_quick_power(matrix a,ll k ,ll y)
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(int i=0;i<=15;i++)
        b.mat[i][i]=1;
    while(k)
    {
        if(k%2==1)  b=matrix_mul(b,a,y);
        k=k/2;
        a=matrix_mul(a,a,y);
    }
    return b;

}

matrix a,b,c;
int main(void)
{
    ll n,k;
    cin >> n >>k;
    memset(a.mat,0,sizeof(a.mat));
    for(int i=0;i<=15;i++)
        for(int j=i-1;j<=i-1+2;j++)
            if(j>=0 && j<=15)
                a.mat[i][j]=1;
    /*for(int i=0;i<=15;i++)
    {
        for(int j=0;j<=15;j++)
            printf("%d ",a.mat[i][j]);
        puts("");
    }*/
    c.mat[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        ll  l,r,y;
        scanf("%I64d%I64d%I64d",&l,&r,&y);
        if(r>k)  r=k;
        for(int i=y+1;i<=18;i++)    c.mat[0][i]=0;
        b=matrix_quick_power(a,r-l,y);
        c=matrix_mul(b,c,y);
    }
    printf("%I64d\n",c.mat[0][0]%MOD);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

typedef struct
{
    ll mat[20][20];
}matrix;

matrix matrix_mul(matrix a,matrix b,ll y)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(ll i=0;i<=y;i++)
    {
        for(ll j=0;j<=y;j++)
        {
            for(ll k=0;k<=y;k++)
            {
                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
            }
        }
    }
    return c;
}

matrix matrix_quick_power(matrix a,ll k ,ll y)
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(int i=0;i<=15;i++)
        b.mat[i][i]=1;
    while(k)
    {
        if(k%2==1)  b=matrix_mul(b,a,y);
        k=k/2;
        a=matrix_mul(a,a,y);
    }
    return b;

}

matrix a,b,c;
int main(void)
{
    ll n,k;
    cin >> n >>k;
    memset(a.mat,0,sizeof(a.mat));
    for(int i=0;i<=15;i++)
        for(int j=i-1;j<=i-1+2;j++)
            if(j>=0 && j<=15)
                a.mat[i][j]=1;
    /*for(int i=0;i<=15;i++)
    {
        for(int j=0;j<=15;j++)
            printf("%d ",a.mat[i][j]);
        puts("");
    }*/
    c.mat[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        ll  l,r,y;
        scanf("%I64d%I64d%I64d",&l,&r,&y);
        if(r>k)  r=k;
        b=matrix_quick_power(a,r-l,y);
        c=matrix_mul(b,c,y);
    }
    printf("%I64d\n",c.mat[0][0]);
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
//
//using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

/*** Typedef ***/
typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
typedef    long double   ld;

//typedef    unordered_set<ll>   usll;
typedef    unordered_multiset<ll,ll>   umsll;
/* Special functions:

        find_by_order(k) --> returns iterator to the kth largest element counting from 0
        order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/
/*** Loops ***/
#define ffit(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
#define F(a,b) for(int i = a; i < b; i++)
#define ff1(i,a,b) for(ll i = a; i < b; i++)
#define ff2(i,a,b) for(ll i = b-1; i >= a; i--)
#define ff3(i,a,b,m) for(ll i = a; i <b; i+=m)
#define ipar(ar,n) vll ar(n); ff1(i,0,n) cin>>ar[i];
#define opar(ar,n) ff1(i,0,n) cout<<ar[i]<<" ";
#define vo(ar,n) ff1(i,0,n) cout<<ar[i]<<" ";
/*** Define Values ***/

#define PI acos(-1.0)
#define eps 1e-7
#define size1 15
#define mdd 1000000007
/*** STLs ***/
#define sf(a)            scanf("%lld",&a)
#define sff(a,b)         scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)
#define cu continue
#define br break
#define rsz resize
#define ins insert
#define ft front
#define bk back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define ff first
#define ss second
#define rr return
#define nxt   next_permutation
//#define mp make_pair
#define mem(name, value) memset(name, value, sizeof(name))
#define mxar(a,n) *max_element(a,a+n)
#define mnar(a,n) *min_element(a,a+n)
#define arr_sm(a,n) accumulate(a,a+n,0)
#define vc_sm(ve) accumulate(ve.begin(),ve.end(),0)
#define mx_vc(ve) *max_element(ve.begin(),ve.end())
#define mn_vc(ve) *min_element(ve.begin(),ve.end())
//#define endl " \n"

/*** STLs ***/
typedef vector <ll> vll;
#define vvll vector<vll>
typedef set <ll> sll;
typedef multiset <ll> msll;
typedef multimap <ll,ll> mpll;
typedef queue <ll> qll;
typedef map <ll, ll> mll;
typedef pair <ll, ll> pll;
typedef vector <pair <ll, ll> > vpll;
typedef priority_queue <pair <ll, ll> > pqpll;
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define ml unordered_map<ll,ll>
///typedef    unordered_multiset<int>   umsi;
typedef    unordered_set<ll>   us;

///vector<vector<int>> v(10, vector<int>(20,500)); 2d vector initialization. of 10 rows and 20 columns, with value 500.

/*** Sorts ***/
#define czero(n)                    __builtin_popcountll(n) // count the number of 1's in the bit representation of a number
#define cenzero(n)                  __builtin_ctzll(n) // count the number of zereos from the end of the bit representation of a number until a 1
#define countbegzero(n)             __builtin_clzxll(n)
#define checkparity(n)              __builtin_parityll(n)// checks whether the number of bits are even or odd
#define all(v) (v).begin(), (v).end()
#define rev(v) reverse(all(v))
#define srt(v) sort(all(v))
#define srtgrt(v) sort(all(v), greater<ll>())
#define prsrt greater<pair<int,int>>()
/*** Some Prints ***/
#define ip(n) ll n; cin>>n;
#define is(s) string s; cin>>s;
#define ipp(n,k) ll n; cin>>n; ll k; cin>>k;
#define ip3(n,m,k) ll n; cin>>n;ll m;cin>>m; ll k; cin>>k;
#define kl    '\n'
#define no cout << "NO" << '\n'
#define yes cout << "YES" << '\n'
#define case cout << "Case " << t++ << ": ";
#define ps(x,y)         fixed<<setprecision(y)<<x
#define inf             1e18
#define w(x)             ll x; cin>>x; while(x--)
#define go(i, a) for (auto &i : a)
#define go1(it, v) for(auto it = v.begin(); it != v.end(); ++it)
//#define mk(arr,n,type) type *arr=new type[n];
#define sz(obj) (int(obj.size()))
#define ipve(vc,n) vll vc(n); ff1(i,0,n){cin>>vc[i];}
#define vi(vc,n) vll vc; ff1(i,0,n){ip(x);vc.pb(x);}
#define cer cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
#define depressed()                    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
//bool ans = all_of(arr, arr + 6, [](int x) {return x & 1; } );
const double pi = 3.141592653589793238460;
#define TRC
#ifdef TRC
#define trc(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trc(...)
#endif
    ll power(ll x, ll y)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
    {
        if(y > 0)
            return (x * temp * temp);
        else
            return ((temp * temp) / x);
    }
}
 ll cbtr(ll n,ll m)
{
  if(m>n) rr 0;
    ll sm = 1;
    ff1(i,1,m+1)
    {
        sm*=(n-i+1);
        sm/=i;
    }
    return sm;
}

void fun()
{
  ip(n);
  vll v[300005];
  vll b;
  ff1(i,0,n)
  {
    ip(x);
    b.pb(x);
    v[x].pb(i);
  }
  vll a(1e6,inf);
  uniq(b);
 go(ii,b)
  {
    ll mx = 1;
    ll i = ii;
   ff1(j,0,sz(v[i]))
    {
      if(j == 0)
      {
        mx = max(mx,v[i][j]+1ll);
      }
      if(j == sz(v[i])-1){
        mx = max(mx,n-v[i][j]);
      }
      if(j>0){
        mx = max(mx,v[i][j]-v[i][j-1]);
      }
    }
   // trc(mx);
  a[mx] = min(a[mx],i);
  }
  ll fg = 0; ll tm = inf;
  ff1(i,1,n+1)
  {
    if(a[i] == inf && fg == 0)
    {
        cout<<-1<<" ";
    }
    else {
      tm = min(tm,a[i]);
      fg = 1;
      cout<<tm<<" ";
    }
  }
  cout<<kl;

}

int main()
{
// tasklist
// taskkill /f /im mo.exe
    depressed();
   // seive(1e5);
  ll t = 1;
  cin>>t;
  while(t--)
  {
     fun();
  }
  //cer;
  return 0;
}


/**
 *  author: O_Ob
 *  created: 09.28.2020 23:37:38
**/

#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int TC;
    cin >> TC;

    while (TC--){
        int n;
        cin >> n;

        vector<int> a(n);
        vector<vector<int>> pos(n + 1);
        for (int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]].push_back(i);
        }

        const int inf = 0x3f3f3f3f;
        vector<int> ans(n + 1, inf);
        for (int i = 1; i <= n; i++){
            if (pos[i].empty())
                continue;

            int mx = pos[i][0];
            for (int j = 1; j < (int) pos[i].size(); j++)
                mx = max(mx, pos[i][j] - pos[i][j - 1] - 1);
            mx = max(mx, n - pos[i].back() - 1);
            ans[mx + 1] = min(ans[mx + 1], i);
        }

        for (int i = 1; i < n; i++)
            ans[i + 1] = min(ans[i + 1], ans[i]);

        for (int i = 1; i <= n; i++)
            cout << (ans[i] == inf ? -1 : ans[i]) << " ";
        cout << '\n';
    }

    return 0;
}


/**
 *  author: O_Ob
 *  created: 09.28.2020 23:37:38
**/

#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int TC;
    cin >> TC;

    while (TC--){
        int n;
        cin >> n;

        vector<int> a(n);
        vector<vector<int>> pos(n + 1);
        for (int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]].push_back(i);
        }

        const int inf = 0x3f3f3f3f;
        vector<int> ans(n + 1, inf);
        for (int i = 1; i <= n; i++){
            if (pos[i].empty())
                continue;

            int mx = pos[i][0];
            for (int j = 1; j < (int) pos[i].size(); j++)
                mx = max(mx, pos[i][j] - pos[i][j - 1]);
            mx = max(mx, n - pos[i].back() - 1);
            ans[mx + 1] = min(ans[mx + 1], i);
        }

        for (int i = 1; i < n; i++)
            ans[i + 1] = min(ans[i + 1], ans[i]);

        for (int i = 1; i <= n; i++)
            cout << (ans[i] == inf ? -1 : ans[i]) << " ";
        cout << '\n';
    }

    return 0;
}


/**
 *  author: O_Ob
 *  created: 09.27.2020 23:31:36
**/

#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int TC;
    cin >> TC;

    while (TC--){
        int n;
        cin >> n;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];

        map<int, int> mp;
        for (int i = 1; i <= n; i++){
            bool ok = false;
            set<int> ans;
            multiset<int> cur;
            for (int j = 0; j < i; j++){
                mp[a[j]] += 1;
                ans.insert(a[j]);
                cur.insert(a[j]);
            }

            for (int j = i; j < n; j++){
                cur.insert(a[j]);
                mp[a[j]] += 1;
                auto it = cur.find(a[j - i]);
                mp[a[j - i]] -= 1;
                cur.erase(it);

                if ((int) ans.size() == 0){
                    break;
                }

                auto itt = ans.begin();
                auto pre = itt;
                while (itt != ans.end()){
                    if (cur.find(*itt) == cur.end()){
                        int x = *itt;
                        ans.erase(itt);
                        itt = pre;
                    }
                    pre = itt;
                    itt++;
                }
            }

            if ((int) ans.size() == 0)
                cout << -1 << " ";
            else{
                int mn = 0x3f3f3f3f, idx = 0x3f3f3f3f;
                for (auto i: ans){
                    if (mn >= mp[i]){
                        mn = mp[i];
                        idx = min(i, idx);
                    }
                }
                cout << endl;

                cout << idx << " ";
            }
        }
        cout << '\n';
    }

    return 0;
}


/**
 *  author: O_Ob
 *  created: 09.27.2020 23:31:36
**/

#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int TC;
    cin >> TC;

    while (TC--){
        int n;
        cin >> n;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];

        map<int, int> mp;
        for (int i = 1; i <= n; i++){
            bool ok = false;
            set<int> ans;
            multiset<int> cur;
            for (int j = 0; j < i; j++){
                mp[a[j]] += 1;
                ans.insert(a[j]);
                cur.insert(a[j]);
            }

            for (int j = i; j < n; j++){
                cur.insert(a[j]);
                mp[a[j]] += 1;
                auto it = cur.find(a[j - i]);
                mp[a[j - i]] -= 1;
                cur.erase(it);

                if ((int) ans.size() == 0){
                    break;
                }

                auto itt = ans.begin();
                auto pre = itt;
                while (itt != ans.end()){
                    if (cur.find(*itt) == cur.end()){
                        int x = *itt;
                        ans.erase(itt);
                        itt = pre;
                    }
                    pre = itt;
                    itt++;
                }
            }

            if ((int) ans.size() == 0)
                cout << -1 << " ";
            else{
                int mn = 0x3f3f3f3f, idx = 0x3f3f3f3f;
                for (auto ii: ans){
                    if (mn >= mp[ii]){
                        mn = mp[ii];
                        idx = min(ii, idx);
                    }
                }
                cout << idx << " ";
            }
        }
        cout << '\n';
    }

    return 0;
}


/**
 *  author: O_Ob
 *  created: 09.27.2020 23:31:36
**/

#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int TC;
    cin >> TC;

    while (TC--){
        int n;
        cin >> n;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];

        map<int, int> mp;
        for (int i = 1; i <= n; i++){
            set<int> ans;
            multiset<int> cur;
            for (int j = 0; j < i; j++){
                mp[a[j]] += 1;
                ans.insert(a[j]);
                cur.insert(a[j]);
            }

            for (int j = i; j < n; j++){
                cur.insert(a[j]);
                mp[a[j]] += 1;
                auto it = cur.find(a[j - i]);
                mp[a[j - i]] -= 1;
                cur.erase(it);

                if ((int) ans.size() == 0){
                    break;
                }

                set<int> tmp = ans;
                for (auto it : tmp){
                    if (cur.find(it) == cur.end()){
                        ans.erase(it);
                    }
                }
            }

            if ((int) ans.size() == 0)
                cout << -1 << " ";
            else{
                int mn = 0x3f3f3f3f, idx = 0x3f3f3f3f;
                for (auto ii: ans){
                    if (mn >= mp[ii]){
                        mn = mp[ii];
                        idx = min(ii, idx);
                    }
                }
                cout << idx << " ";
            }
        }
        cout << '\n';
    }

    return 0;
}


/**
 *  author: O_Ob
 *  created: 09.27.2020 23:31:36
**/

#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int TC;
    cin >> TC;

    while (TC--){
        int n;
        cin >> n;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];

        map<int, int> mp;
        for (int i = 1; i <= n; i++){
            bool ok = false;
            set<int> ans;
            multiset<int> cur;
            for (int j = 0; j < i; j++){
                mp[a[j]] += 1;
                ans.insert(a[j]);
                cur.insert(a[j]);
            }

            for (int j = i; j < n; j++){
                cur.insert(a[j]);
                mp[a[j]] += 1;
                auto it = cur.find(a[j - i]);
                mp[a[j - i]] -= 1;
                cur.erase(it);

                auto itt = ans.begin();
                while (itt != ans.end()){
                    if (cur.find(*itt) == cur.end()){
                        ans.erase(itt);
                    }
                    itt++;
                }
            }

            if ((int) ans.size() == 0)
                cout << -1 << " ";
            else{
                int mn = 0x3f3f3f3f, idx = 0x3f3f3f3f;
                for (auto i: ans){
                    if (mn >= mp[i]){
                        mn = mp[i];
                        idx = min(i, idx);
                    }
                }

                cout << idx << " ";
            }
        }
        cout << '\n';
    }

    return 0;
}


/**
 *  author: O_Ob
 *  created: 09.27.2020 23:31:36
**/

#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int TC;
    cin >> TC;

    while (TC--){
        int n;
        cin >> n;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];

        map<int, int> mp;
        for (int i = 1; i <= n; i++){
            bool ok = false;
            set<int> ans;
            multiset<int> cur;
            for (int j = 0; j < i; j++){
                mp[a[j]] += 1;
                ans.insert(a[j]);
                cur.insert(a[j]);
            }

            for (int j = i; j < n; j++){
                cur.insert(a[j]);
                mp[a[j]] += 1;
                auto it = cur.find(a[j - i]);
                mp[a[j - i]] -= 1;
                cur.erase(it);

                auto itt = ans.begin();
                auto pre = itt;
                while (itt != ans.end()){
                    if (cur.find(*itt) == cur.end()){
                        int x = *itt;
                        ans.erase(itt);
                        itt = pre;
                    }
                    pre = itt;
                    itt++;
                }
            }

            if ((int) ans.size() == 0)
                cout << -1 << " ";
            else{
                int mn = 0x3f3f3f3f, idx = 0x3f3f3f3f;
                for (auto i: ans){
                    if (mn >= mp[i]){
                        mn = mp[i];
                        idx = min(i, idx);
                    }
                }

                cout << idx << " ";
            }
        }
        cout << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#define ll long long int
#define ull  unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a)  sort(a,a+n,greater<int>())
#define sortA(a)  sort(a,a+n)
//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define vi  vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
//map
#define um unordered_map
#define fi first
#define se second
#define mapit unordered_map<int,int>:: iterator
//1e18
#define pi 3.1415926536
#define mod 1000000007
#define imax INT_MAX
#define imin INT_MIN
using namespace std;
int i,j;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; for(i=0;i<n;i++)cin>>a[i];
        for(j=1;j<=n;j++)
        {
            int min=INT_MAX; unordered_map<int,int>m;
            for(i=0;i<n;i++)
            {
                int y=i-j+1;
                if(y<0)
                {
                    y=0;
                }
                int z=j+i-1;
                if(z>n-1)
                {
                    z=n-1;
                }
                m[a[i]]+=(z-y+1);
                // cout<<y<<" "<<z-y+1<<" ";
                if(m[a[i]]>=n)
                {
                    if(a[i]<min)min=a[i];
                }
            }
            if(min!=INT_MAX)
            cout<<min<<" ";
            else cout<<-1<<" ";
        }

        cout<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#define ll long long int
#define ull  unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a)  sort(a,a+n,greater<int>())
#define sortA(a)  sort(a,a+n)
//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define vi  vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
//map
#define um unordered_map
#define fi first
#define se second
#define mapit unordered_map<int,int>:: iterator
//1e18
#define pi 3.1415926536
#define mod 1000000007
#define imax INT_MAX
#define imin INT_MIN
using namespace std;
int i,j,k;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; for(i=0;i<n;i++)cin>>a[i];
        for(j=1;j<=n;j++)
        {
            vector<vector<int>>v(n+1,vector<int>(n+1,0));
            for(i=0;i<n;i++)
            {
                int y=i-j+1;
                if(y<0)
                {
                    y=0;
                }
                int z=j+i-1;
                if(z>n-1)
                {
                    z=n-1;
                }
                v[a[i]][y]+=1;
                v[a[i]][z+1]-=1;
            }
            int l=INT_MAX;
            for(i=1;i<=n;i++)
            {
                int f=0;
                if(v[i][0]==0)
                {
                    continue;
                }
                for(k=1;k<n;k++)
                {
                    v[i][k]+=v[i][k-1];
                    if(v[i][k]==0)
                    {
                        f=-1;
                        break;
                    }
                }
                if(f==-1)
                {
                    continue;
                }
                else
                {
                    l=i;
                    break;
                }

            }
            if(l!=INT_MAX)
            cout<<l<<" ";
            else
            cout<<-1<<" ";
        }

        cout<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#define ll long long int
#define ull  unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a)  sort(a,a+n,greater<int>())
#define sortA(a)  sort(a,a+n)
//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define vi  vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
//map
#define um unordered_map
#define fi first
#define se second
#define mapit unordered_map<int,int>:: iterator
//1e18
#define pi 3.1415926536
#define mod 1000000007
#define imax INT_MAX
#define imin INT_MIN
using namespace std;
int i,j,k;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; for(i=0;i<n;i++)cin>>a[i];
        for(j=1;j<=n;j++)
        {
            vector<unordered_set<int>>v(n+1,unordered_set<int>());
            int l=INT_MAX;
            for(i=0;i<n;i++)
            {
                int y=i-j+1;
                if(y<0)
                {
                    y=0;
                }
                int d=i,x=y;
                if(i>n-j)
                {
                    d=n-j;
                }
                if(y>n-j)
                {
                    x=n-j;
                }
                for(k=x;k<=d;k++)
                {
                    v[a[i]].insert(k);
                }
                if(v[a[i]].size()==n-j+1)
                {
                    if(a[i]<l)l=a[i];
                }

            }
            if(l!=INT_MAX)
            cout<<l<<" ";
            else
            cout<<-1<<" ";
        }

        cout<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#define ll long long int
#define ull  unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a)  sort(a,a+n,greater<int>())
#define sortA(a)  sort(a,a+n)
//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define vi  vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
//map
#define um unordered_map
#define fi first
#define se second
#define mapit unordered_map<int,int>:: iterator
//1e18
#define pi 3.1415926536
#define mod 1000000007
#define imax INT_MAX
#define imin INT_MIN
using namespace std;
int i,j;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; for(i=0;i<n;i++)cin>>a[i];
        vector<int>l(n+1,-1),c(n+1,0),d(n+1,-1);
        for(i=0;i<n;i++)
        {
            c[a[i]]=max(c[a[i]],i-l[a[i]]);
            l[a[i]]=i;
        }
        for(i=1;i<=n;i++)
        {
            c[i]=max(c[i],n-l[i]);
            for(j=c[i];j<=n;j++)
            {
                if(d[j]!=-1)
                {
                    break;
                }
                d[j]=i;
            }

        }
        for(i=1;i<=n;i++)
        cout<<d[i]<<" ";

        cout<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#define ll long long int
#define ull  unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a)  sort(a,a+n,greater<int>())
#define sortA(a)  sort(a,a+n)
//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define vi  vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
//map
#define um unordered_map
#define fi first
#define se second
#define mapit unordered_map<int,int>:: iterator
//1e18
#define pi 3.1415926536
#define mod 1000000007
#define imax INT_MAX
#define imin INT_MIN
using namespace std;
int i,j;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; for(i=0;i<n;i++)cin>>a[i];
        vector<int>l(n+1,0),c(n+1,0),d(n+1,-1);
        for(i=0;i<n;i++)
        {
            c[a[i]]=max(c[a[i]],i-l[a[i]]);
            l[a[i]]=i;
        }
        for(i=1;i<=n;i++)
        {
            c[i]=max(c[i],n-l[i]);
            for(j=c[i];j<=n;j++)
            {
                if(d[j]!=-1)
                {
                    break;
                }
                d[j]=i;
            }

        }
        for(i=1;i<=n;i++)
        cout<<d[i]<<" ";

        cout<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#define ll long long int
#define ull  unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a)  sort(a,a+n,greater<int>())
#define sortA(a)  sort(a,a+n)
//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define vi  vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
//map
#define um unordered_map
#define fi first
#define se second
#define mapit unordered_map<int,int>:: iterator
//1e18
#define pi 3.1415926536
#define mod 1000000007
#define imax INT_MAX
#define imin INT_MIN
using namespace std;
int i,j,k;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n; cin>>n; int a[n]; for(i=0;i<n;i++)cin>>a[i];
        for(j=1;j<=n;j++)
        {
            vector<vector<int>>v(n+1,vector<int>(n+1,0));
            for(i=0;i<n;i++)
            {
                int y=i-j+1;
                if(y<0)
                {
                    y=0;
                }
                // cout<<a[i]<<" "<<y<<" "<<i<<" | ";
                v[a[i]][y]+=1;
                v[a[i]][i+1]-=1;
            }
            // cout<<"wtf";
            int l=INT_MAX;
            for(i=1;i<=n;i++)
            {
                int f=0;
                if(v[i][0]==0)
                {
                    continue;
                }
                for(k=1;k<=n-j;k++)
                {
                    v[i][k]+=v[i][k-1];
                    if(v[i][k]==0)
                    {
                        f=-1;
                        break;
                    }
                }
                if(f==-1)
                {
                    continue;
                }
                else
                {
                    l=i;
                    break;
                }

            }
            if(l!=INT_MAX)
            cout<<l<<" ";
            else
            cout<<-1<<" ";
            // cout<<"\n";
        }

        cout<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, n;
int a[N], pr[N], dis[N], ans[N], la[N];

signed main(){
    IOS;
    cin >> t;
    while(t --){
    cin >> n;
    for(int i = 1; i <= n; i ++) cin >> a[i];
    for(int i = 0; i <= n; i ++)
        ans[i] = dis[i] = pr[i] = la[i] = 0;
    for(int i = 1; i <= n; i ++) ans[i] = 1e9;
    for(int i = 1; i <= n; i ++){
      dis[a[i]] = max(dis[a[i]], i-pr[a[i]]);
      la[a[i]] = i, pr[a[i]] = i;
    }
    for(int i = 1; i <= n; i ++)
        if(la[i]) dis[i] = max(dis[i], n+1-la[i]);
    for(int i = 1; i <= n; i ++)
        ans[dis[i]] = min(ans[dis[i]], i);
    for(int i = 2; i <= n; i ++)
        ans[i] = min(ans[i], ans[i-1]);
    for(int i = 1; i <= n; i ++)
        cout<<(ans[i]==1e9?-1:ans[i])<<' '; cout<<'\n';
  }
  return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, n, a[300010],b[300010];

void solve(){
    map<int,int> mp,mp2,pre;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			mp2[a[i]]++;
			b[i]=inf;
		}
		for(int i=1;i<=n;i++){
			mp[a[i]]=max(i-pre[a[i]],mp[a[i]]);
			pre[a[i]]=i;
		}
		for(int i=1;i<=n;i++){
			if(mp2[a[i]]==1) mp[a[i]]=max(mp[a[i]],n-i+1);
			else mp2[a[i]]--;
			}
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++){
			b[mp[a[i]]]=min(a[i],b[mp[a[i]]]);
		}
		int s=inf;
		for(int i=1;i<=n;i++){
			if(b[i]==inf&&s==inf){
				cout<<-1<<" \n"[i==n];
			}
			else{
				s=min(s,b[i]);
				cout<<s<<" \n"[i==n];
			}
		}
}

signed main(){
	IOS;
	cin>>t;
	while(t--){
		cin>>n;
		solve();
	}
    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, n, ans[N];
vector<int> v[N];
signed main()
{
  IOS;
  cin >> t;
  while(t --){
  	cin >> n;
  	for(int i = 0; i <= n+10; i ++) ans[i] = inf, v[i].clear();
  	for(int i = 1; i <= n; i ++){
  		int x; cin >> x;
        v[x].push_back(i);
	}
	for(int i = 1; i <= n; i ++){
		if(!v[i].empty()){
			int mx = 0;
			for(int j = 1; j < v[i].size(); j ++)
				mx = max(mx, v[i][j]-v[i][j-1]);
			mx = max(mx, v[i].front());
			mx = max(mx, n-v[i].back()+1);
			ans[mx] = min(ans[mx],i);
		}
	}
	for(int i = 2; i <= n; i ++) ans[i] = min(ans[i], ans[i-1]);
	for(int i = 1; i <= n; i ++){
		if(ans[i] != inf) cout << ans[i] << " \n"[i==n];
		else cout << "-1" << " \n"[i==n];
	}
  }
return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
//#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 3e5 + 100;

ll t, n, ans[N];
vector<ll> v[N];
int main()
{
  IOS;
  cin >> t;
  while(t --){
  	cin >> n;
  	for(ll i = 0; i <= n+10; i ++) ans[i] = inf, v[i].clear();
  	for(ll i = 1; i <= n; i ++){
  		ll x; cin >> x;
        v[x].push_back(i);
	}
	for(ll i = 1; i <= n; i ++){
		if(!v[i].empty()){
			ll mx = 0;
			for(ll j = 1; j < v[i].size(); j ++)
				mx = max(mx, v[i][j]-v[i][j-1]);
			mx = max(mx, v[i].front());
			mx = max(mx, n-v[i].back()+1);
			ans[mx] = min(ans[mx],i);
		}
	}
	for(ll i = 2; i <= n; i ++) ans[i] = min(ans[i], ans[i-1]);
	for(ll i = 1; i <= n; i ++){
		if(ans[i] != inf) cout << ans[i] << " ";
		else cout << "-1" << " ";
	}
	cout << endl;
  }
return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 3e5 + 5;

int t, n, ans[N];
vector<int> v[N];
signed main()
{
  IOS;
  cin >> t;
  while(t --){
  	cin >> n;
  	for(int i = 0; i <= n+10; i ++) ans[i] = inf, v[i].clear();
  	for(int i = 1; i <= n; i ++){
  		int x; cin >> x;
        v[x].push_back(i);
	}
	for(int i = 1; i <= n; i ++){
		if(!v[i].empty()){
			int mx = 0;
			for(int j = 1; j < v[i].size(); j ++)
				mx = max(mx, v[i][j]-v[i][j-1]);
			mx = max(mx, v[i].front());
			mx = max(mx, n-v[i].back()+1);
			ans[mx] = min(ans[mx],i);
		}
	}
	for(int i = 2; i <= n; i ++) ans[i] = min(ans[i], ans[i-1]);
	for(int i = 1; i <= n; i ++){
		if(ans[i] != inf) cout << ans[i] << " \n"[i==n];
		else cout << "-1" << " \n"[i==n];
	}
  }
return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e5+100;
typedef long long LL;
const LL inf=1e18;
LL ans[maxn];
vector<LL>v[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--)
  {
  	LL n;cin>>n;
  	for(LL i=0;i<=n+10;i++) ans[i]=inf,v[i].clear();
  	for(LL i=1;i<=n;i++){
  		LL x;cin>>x;v[x].push_back(i);
	}
	for(LL i=1;i<=n;i++)
	{
		if(!v[i].empty())
		{
			LL mx=0;
			for(LL j=1;j<v[i].size();j++)
			{
				mx=max(mx,v[i][j]-v[i][j-1]);
			}
			mx=max(mx,v[i].front());
			mx=max(mx,n-v[i].back()+1);
			ans[mx]=min(ans[mx],i);
		}
	}
	for(LL i=2;i<=n;i++) ans[i]=min(ans[i],ans[i-1]);
	for(LL i=1;i<=n;i++){
		if(ans[i]<inf) cout<<ans[i]<<" ";
		else cout<<"-1"<<" ";
	}
	cout<<endl;
  }
return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 3e5 + 5;

int t, n, ans[N];
vector<int> v[N];
signed main()
{
  IOS;
  cin >> t;
  while(t --){
  	cin >> n;
  	for(int i = 0; i <= n+10; i ++) ans[i] = inf, v[i].clear();
  	for(int i = 1; i <= n; i ++){
  		int x; cin >> x;
        v[x].push_back(i);
	}
	for(int i = 1; i <= n; i ++){
		if(!v[i].empty()){
			int mx = 0;
			for(int j = 1; j < v[i].size(); j ++)
				mx = max(mx, v[i][j]-v[i][j-1]);
			mx = max(mx, v[i].front());
			mx = max(mx, n-v[i].back()+1);
			ans[mx] = min(ans[mx],i);
		}
	}
	for(int i = 2; i <= n; i ++) ans[i] = min(ans[i], ans[i-1]);
	for(int i = 1; i <= n; i ++){
		if(ans[i] != inf) cout << ans[i] << " ";
		else cout << "-1" << " ";
	}
	cout << endl;
  }
return 0;
}


/*
Author- Aasav Badera
Time- Wed Sep 30 10:59:00 2020
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include <sys/resource.h>
#define meminc             rlimit rlim;if (getrlimit(RLIMIT_STACK, &rlim)) return 1;rlim.rlim_cur = 268435456;if (setrlimit(RLIMIT_STACK, &rlim)) return 2;
#else
#define meminc
#endif
#ifdef LOCAL
#define dbg(args...)       { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#else
#define dbg(args...)
#endif
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
#define ll                 int
#define vi                 vector<int >
#define ordered_set        tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// -------------------Standard Traversal Moves---------------------
// vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1};
// vi fx = {2, -2, 2, -2,  1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2};
// vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1};
// ----------------------------------------------------------------

#define rep(i, a, b)       for(int  i=a;i<b;i++)
#define all(a)             (a).begin(),(a).end()
#define sz(x)              (int )x.size()
#define yes                cout << "YES" << endl
#define no                 cout << "NO" << endl
#define endl               '\n'
// const int hell = (int)998244353;
const int hell = (int)1e9 + 7;
const long long int inf = 3223372036854775807;
const double PI = 3.14159265;
const int N = (int) 1e6 + 5;
int n, m, k, a[N];

struct item{
    // int m, c;
    // long long int su;
    // long long int mex, pref, suf, sum;
    int val;
    // override assignment operator and default constructor and also remember to override + operator to get merge operation sorted.
    item(){
        val = hell;
    }
    item(int ok)
    {
        val = ok;
    }
    item operator+(const item &op1)
    {
        item ret;
        ret.val = min(op1.val ,val);
        return ret;
    }
};
//O(N) preprocessing time and O(logN) query time amortized.
// take care this implementation is 0 indexed!
// segtree st;
// st.init(n);
// st.build(v);
template<class itemType, class B>
struct segtree{
    int sz;
    vector<itemType> values;
    itemType NEUTRAL_ELEMENT;
    itemType merge(itemType fi, itemType se)
    {
        // if(fi.m < se.m)
        // {
        //     return fi;
        // }
        // if(fi.m > se.m)
        // {
        //     return se;
        // }
        // return {fi.m, fi.c + se.c};
        // return {min(fi.su, se.su)};
        // return {
        //     max({fi.mex ,se.mex ,fi.suf + se.pref}),
        //     max({fi.pref, fi.sum + se.pref}),
        //     max({se.suf, se.sum + fi.suf}),
        //     fi.sum + se.sum
        // };
        return {fi.val + se.val};
    }

    itemType single(B val)
    {
        // if(val > 0)return {val, val, val, val};
        // return {0, 0 , 0, val};
        return {val};
    }
    inline int ri(int _gg){return _gg * 2 + 2;}
    inline int le(int _gg){return _gg * 2 + 1;}
    void init(int new_size)
    {
        sz = 1;
        while(sz < new_size)sz <<= 1;
        values.resize(sz << 1);
    }
    void build(vector<B> &v, int x, int lx, int rx)
    {
        if(rx - lx == 1)
        {
            if(lx < sz(v))
            {
                values[x] = v[lx];
            }
            return ;
        }
        int mx = (lx + rx) >> 1;
        build(v, le(x), lx, mx);
        build(v, ri(x), mx, rx);

        values[x] = values[le(x)] + values[ri(x)];
    }
    void build(vector<B> &v)
    {
        /****************************************************************************/
        // REMEMBER TO CHANGE IT IF OUR FIRST ARGUMENT IS NOT ITEM WHICH IS RARE THO
        // initializing neutral value
        NEUTRAL_ELEMENT = item();
        /*****************************************************************************/
        int _n = v.size();
        init(_n);
        build(v, 0, 0, sz);
    }
    void set(int i, B val, int x, int lx, int rx)
    {
        if(rx - lx == 1)
        {
            values[x] = val;
            return ;
        }
        int mx = (lx + rx) >> 1;
        if(i < mx)
        {
            set(i, val, le(x), lx, mx);
        }
        else
            set(i, val, ri(x), mx, rx);
        values[x] = values[le(x)] + values[ri(x)];
    }
    void set(int i, B val)
    {
        set(i, val, 0, 0, sz);
    }
    itemType calc(int l, int r, int x, int lx, int rx)
    {
        if(lx >= r or l >= rx)return NEUTRAL_ELEMENT;
        if(lx >= l and rx <= r)return values[x];
        int mx = (lx + rx) >> 1;
        itemType f1 = calc(l ,r ,le(x), lx, mx);
        itemType f2 = calc(l ,r ,ri(x), mx, rx);
        return f1 + f2;
    }
    itemType calc(int l, int r)
    {
        return calc(l, r, 0, 0, sz);
    }
};

int32_t main()
{
    meminc;
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cerr.precision(10);
    cout.precision(25);
    cout << fixed;
    #ifdef LOCAL
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
    #endif
    int tests = 1;
    cin >> tests;
    rep(test, 1, tests+1)
    {
        cin >> n;
        segtree<item, int> st;
        vector<int> v(n, 0);
        st.build(v);
        rep(i, 0, n)cin >> a[i];
        if(n == 1)
        {
        	cout << a[0] << endl;
        	continue;
        }
        int ansyet = -1;
        rep(i, 0, n)
        {
        	k = i + 1;
        	st.set(i, a[i]);
        	int j = k + i;
        	int key = 1;
        	while(j < n and a[j] == a[i])
        	{
        		j += k;
        	}
        	if(j < n)key = 0;
        	if(key and (ansyet > a[i] or ansyet == -1)){
        		ansyet = a[i];
        	}
        	if(key)
        	{
        		if(k + i < n);
        		else
        		{
        			int ok = st.calc(n - i - 1, i + 1).val;
        			ansyet = min(ansyet, ok);
        		}
        	}
        	cout << ansyet << " ";
        }
        cout << endl;
    }
    #ifdef LOCAL
        // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}