Problem:
Problem L: Count Pow Sum
Problem
$Q$ 個の3つの整数の組 $(a_i,l_i,r_i) (1 \leq i \leq Q)$ が与えられる。
それぞれの組に対し、
	$\displaystyle \sum_{k=l_i}^{r_i}\left\lfloor{\left(a_i+\sqrt{a_i^2-1}\right)^k}\right\rfloor$ を $10^9+7$
	で割った余りを求めよ。
但し、$\lfloor{x}\rfloor$ は $x$ 以下である最大の整数を表す。
Constraints
入力は以下の条件を満たす。
$1 \leq Q \leq 10000$
$1 \leq a_i \leq 10^9$
$0 \leq l_i \leq r_i \leq 10^9$
入力は全て整数である。
Input
入力は以下の形式で与えられる。
$Q$
$a_1$ $l_1$ $r_1$
$a_2$ $l_2$ $r_2$
$\vdots$
$a_Q$ $l_Q$ $r_Q$
Output
$Q$ 個のクエリそれぞれに対し、 $\displaystyle \sum_{k=l_i}^{r_i}\left\lfloor{\left(a_i+\sqrt{a_i^2-1}\right)^k}\right\rfloor$ を
	$10^9+7$ で割った余りを出力せよ。
末尾の改行を忘れないこと。
Sample Input 1
5
2 0 0
2 1 1
2 2 2
2 3 3
2 2 3
Sample Output 1
1
3
13
51
64
$\left\lfloor\left(2+\sqrt{3}\right)^0\right\rfloor=1$
$\left\lfloor\left(2+\sqrt{3}\right)^1\right\rfloor=\left\lfloor 2+\sqrt{3}\right\rfloor=3$
$\left\lfloor\left(2+\sqrt{3}\right)^2\right\rfloor=\left\lfloor 7+4\sqrt{3}\right\rfloor=13$
$\left\lfloor\left(2+\sqrt{3}\right)^3\right\rfloor=\left\lfloor 26+15\sqrt{3}\right\rfloor=51$
より、
	それぞれ
$1$
$3$
$13$
$51$
$13+51=64$
が答えになります。
Sample Input 2
10
459404297 517642810 741889747
581992024 866504331 929744396
222841627 420642437 697287723
683338343 430705406 509597170
390891363 475139614 643827664
855081312 565758976 925253656
883384773 466157419 667964073
962783174 289373011 576778244
843984728 54647959 959764601
825451551 69560986 411653622
Sample Output 2
818692813
502289003
874771119
265904125
166949046
644621561
489591300
573971217
378976183
486013871
$10^9+7$ で割った余りを求めてください。

Submitted solutions:

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
typedef long long int ll;
constexpr ll mod=1e9+7;

struct mat{
	ll x[3][3];
	friend mat operator*(mat &a,mat &b){
		mat c;
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				c.x[i][j]=0;
				for(int k=0;k<3;k++){
					(c.x[i][j]+=a.x[i][k]*b.x[k][j]%mod)%=mod;
				}
			}
		}
		return c;
	}
};

mat x,res;

ll mod_pow(int n){
	while(n){
		if(n%2)res=x*res;
		x=x*x;
		n>>=1;
	}
	return res.x[0][0];
}

void init(ll a){
	for(int i=0;i<3;i++)for(int j=0;j<3;j++)x.x[i][j]=res.x[i][j]=0;
	res.x[0][0]=2,res.x[1][0]=1;
	x.x[0][0]=1,x.x[0][1]=2*a,x.x[0][2]=2*(a*a-1);
	x.x[1][1]=a,x.x[1][2]=a*a-1;
	x.x[2][1]=1,x.x[2][2]=a;
	for(int i=0;i<3;i++)for(int j=0;j<3;j++)x.x[i][j]%=mod;
	return;
}

ll solve(int a,int l,int r){
	if(a==1)return (r-l+1);
	init(a); ll R=mod_pow(r);
	init(a); ll L=(l?mod_pow(l-1):0);
	ll val=(R-L-(r-l+1))%mod;
	return (val+mod)%mod;
}

int main(){
	int q; cin >> q;
	while(q--){
		int a,l,r; cin >> a >> l >> r;
		cout << solve(a,l,r) << "\n";
	}
}
accuracy: 1
time_ms: 60
memory_kb: 3440
final_rank: 7

code_snippet: #line 1 "a.cpp"
#include<iostream>
using namespace std;
#line 3 "/home/kotatsugame/library/math/modint.cpp"
#include<utility>
template<int m>
struct modint{
	unsigned int x;
	constexpr modint()noexcept:x(){}
	template<typename T>
	constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){}
	constexpr unsigned int val()const noexcept{return x;}
	constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;}
	constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;}
	constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;}
	constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;}
	constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;}
	constexpr modint&operator-=(const modint&a)noexcept{if(x<a.x)x+=m;x-=a.x;return*this;}
	constexpr modint&operator*=(const modint&a)noexcept{x=(unsigned long long)x*a.x%m;return*this;}
	constexpr modint&operator/=(const modint&a)noexcept{return*this*=a.inv();}
	constexpr modint operator+()const noexcept{return*this;}
	constexpr modint operator-()const noexcept{return modint()-*this;}
	constexpr modint pow(long long n)const noexcept
	{
		if(n<0)return pow(-n).inv();
		modint x=*this,r=1;
		for(;n;x*=x,n>>=1)if(n&1)r*=x;
		return r;
	}
	constexpr modint inv()const noexcept
	{
		int s=x,t=m,x=1,u=0;
		while(t)
		{
			int k=s/t;
			s-=k*t;
			swap(s,t);
			x-=k*u;
			swap(x,u);
		}
		return modint(x);
	}
	friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;}
	friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;}
	friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;}
	friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;}
	friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;}
	friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;}
	friend ostream&operator<<(ostream&os,const modint&a){return os<<a.x;}
	friend istream&operator>>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;}
};
#line 1 "/home/kotatsugame/library/math/squarematrix.cpp"
#include<array>
template<typename T,unsigned int N>
struct Matrix{
	array<array<T,N>,N>dat;
	array<T,N>&operator[](int i){return dat[i];}
	const array<T,N>&operator[](int i)const{return dat[i];}
	static Matrix eye(){
		Matrix res;
		for(int i=0;i<N;i++)res[i][i]=1;
		return res;
	}
	Matrix operator+(const Matrix&A)const{
		Matrix res;
		for(int i=0;i<N;i++)for(int j=0;j<N;j++)
			res[i][j]=dat[i][j]+A[i][j];
		return res;
	}
	Matrix operator*(const Matrix&A)const{
		Matrix res;
		for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=0;k<N;k++)
			res[i][j]+=dat[i][k]*A[k][j];
		return res;
	}
	Matrix pow(long long n)const{
		Matrix a=*this,res=eye();
		for(;n;a=a*a,n>>=1)if(n&1)res=res*a;
		return res;
	}
};
#line 5 "a.cpp"
using mint=modint<(int)1e9+7>;
using mat=Matrix<mint,3>;
int Q,A;
mint f(int N)
{
	if(N<0)return 0;
	mat X;
	X[0][0]=1;
	X[0][1]=1;
	X[1][1]=2*A;
	X[1][2]=-1;
	X[2][1]=1;
	X=X.pow(N);
	return X[0][0]*2+X[0][1]*2*A+X[0][2]*2-N-1;
}
main()
{
	cin>>Q;
	for(;Q--;)
	{
		int L,R;
		cin>>A>>L>>R;
		cout<<f(R)-f(L-1)<<endl;
	}
}
accuracy: 1
time_ms: 70
memory_kb: 3104
final_rank: 2

code_snippet: #pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
// #include<boost/multiprecision/cpp_int.hpp>
// namespace multiprecisioninteger = boost::multiprecision;
// using cint=multiprecisioninteger::cpp_int;
using namespace std;
using ll=long long;
#define double long double
using datas=pair<ll,ll>;
using ddatas=pair<double,double>;
using tdata=pair<ll,datas>;
using vec=vector<ll>;
using mat=vector<vec>;
using pvec=vector<datas>;
using pmat=vector<pvec>;
// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i,a,b) for(i=a;i<(ll)b;++i)
#define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N,0)
#define brep1(i,N) bFor(i,N,1)
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define endl "\n"
#define eb emplace_back
#define print(v) cout<<v<<endl
#define printyes cout<<"Yes"<<endl
#define printno cout<<"No"<<endl
#define printYES cout<<"YES"<<endl
#define printNO cout<<"NO"<<endl
#define output(v) do{bool f=0;for(auto outi:v){cout<<(f?" ":"")<<outi;f=1;}cout<<endl;}while(0)
#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)
const ll mod=1000000007;
// const ll mod=998244353;
const ll inf=1LL<<60;
const double PI = acos(-1);
const double eps = 1e-9;
template<class T> inline bool chmax(T& a,T b){bool x=a<b;if(x)a=b;return x;} 
template<class T> inline bool chmin(T& a,T b){bool x=a>b;if(x)a=b;return x;} 

void startupcpp(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
}

double distance(ddatas x,ddatas y){
  double a=x.first-y.first,b=x.second-y.second;
  return sqrt(a*a+b*b);
}

ll modinv(ll a,ll m=mod) {
  ll b=m,u=1,v=0,t;
  while(b){
    t=a/b;
    a-=t*b; swap(a,b);
    u-=t*v; swap(u,v);
  }
  return (u+m)%m;
}

ll moddevide(ll a,ll b){return (a*modinv(b))%mod;}

vec modncrlistp,modncrlistm;

ll modncr(ll n,ll r){
  if(n<r)return 0;
  ll i,size=modncrlistp.size();
  if(size<=n){
    modncrlistp.resize(n+1);
    modncrlistm.resize(n+1);
    if(!size){
      modncrlistp[0]=modncrlistm[0]=1;
      size++;
    }
    For(i,size,n+1){
      modncrlistp[i]=modncrlistp[i-1]*i%mod;
      modncrlistm[i]=modinv(modncrlistp[i]);
    }
  }
  return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;
}

ll modpow(ll a,ll n,ll m=mod){
  ll res=1;
  while(n>0){
    if(n&1)res=res*a%m;
    a=a*a%m;
    n>>=1;
  }
  return res;
}

ll gcd(ll a,ll b){if(!b)return abs(a);return (a%b==0)?abs(b):gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

ll countdigits(ll n){
  ll ans=0;
  while(n){n/=10;ans++;}
  return ans;
}

ll sumdigits(ll n){
  ll ans=0;
  while(n){ans+=n%10;n/=10;}
  return ans;
}
class matrix{
  mat a;
  ll H,W;
public:
  matrix(mat& g):a(g){
    H=g.size();
    W=g[0].size();
  }
  matrix(ll i,ll j):a(i,vec(j,0)){H=i;W=j;}
  matrix(ll n):a(n,vec(n,0)){H=W=n;}
  inline vec& operator [](int k){
    return a.at(k);
  }
  // matrix operator =(matrix b){
  //   this->a.swap(b->a);
  //   this->H=b.H;
  //   this->W=b.W;
  //   return (*this);
  // }
  matrix operator +=(matrix b){
    ll i,j;
    rep(i,this->H)rep(j,this->W)(*this)[i][j]+=b[i][j];
    return (*this);
  }
  matrix operator -=(matrix b){
    ll i,j;
    rep(i,this->H)rep(j,this->W)(*this)[i][j]-=b[i][j];
    return (*this);
  }
  matrix operator *=(matrix b){
    ll i,j,k;
    assert(this->W==b.H);
    matrix c(this->H,b.W);
    rep(i,this->H)rep(j,b.W){
      c[i][j]=0;
      rep(k,this->W)(c[i][j]+=(*this)[i][k]*b[k][j]%mod)%=mod;
    }
    (*this)=c;
    return (*this);
  }
  matrix operator ^=(ll K){
    assert(this->H==this->W);
    matrix c(this->H);
    ll i;
    rep(i,this->H)c[i][i]=1;
    if(K&1)c*=(*this);
    while(K){
      K>>=1;
      (*this)*=(*this);
      if(K&1)c*=(*this);
    }
    this->a.swap(c.a);
    return (*this);
  }
  matrix operator +(matrix c){
    return matrix(*this)+=c;
  }
  matrix operator -(matrix c){
    return matrix(*this)-=c;
  }
  matrix operator *(matrix c){
    return matrix(*this)*=c;
  }
  matrix operator ^(ll K){
    return matrix(*this)^=K;
  }
  void out(){
    for(auto x:a)output(x);
  }
};

int main(){
  startupcpp();
  int codeforces;cin>>codeforces;while(codeforces--){
  ll i,j,a,l,r,ans;
  cin>>a>>l>>r;
  if(r==0){
    print(1);
    continue;
  }
  if(a==1){
    print(r-l+1);
    continue;
  }
  l+=ans=!l;
  ans-=r-l+1;
  matrix g(2),I(2),v(2,1);
  I[0][0]=I[1][1]=1;
  v[0][0]=-1;v[1][0]=1;
  g[0][0]=2*a%mod;g[0][1]=-1;g[1][0]=1;g[1][1]=0;
  auto xg=I-(g^(r-l+1));
  auto ansg=(g^(l-1))*xg*v;
  ans+=ansg[0][0];
  ans%=mod;
  if(ans<0)ans+=mod;
  print(ans);
}
}
accuracy: 1
time_ms: 160
memory_kb: 3476
final_rank: 14

code_snippet: #include <iostream>
#include <array>


#include <utility>

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }
    static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }
    dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


namespace ac = atcoder;

template <class T, int D>
struct Vector {
    using V = std::array<T, D>;

    V vec;

    // constructor
    Vector(T val = 0) { vec.fill(val); }

    // getter
    T& operator[](int i) { return vec[i]; }
    T operator[](int i) const { return vec[i]; }
    typename V::iterator begin() { return vec.begin(); }
    typename V::iterator end() { return vec.end(); }

    // arithmetic
    Vector operator+(const Vector& v) const { return Vector(*this) += v; }
    Vector operator-(const Vector& v) const { return Vector(*this) -= v; }
    T operator*(const Vector& v) const {
        T ret(0);
        for (int i = 0; i < D; ++i) ret += vec[i] * v[i];
        return ret;
    }

    // compound assignment
    Vector& operator+=(const Vector& v) {
        for (int i = 0; i < D; ++i) vec[i] += v[i];
        return *this;
    }
    Vector& operator-=(const Vector& v) {
        for (int i = 0; i < D; ++i) vec[i] -= v[i];
        return *this;
    }
};

template <class T, int D>
struct Matrix {
    using M = std::array<std::array<T, D>, D>;

    M mat;

    // constructor
    Matrix(T val = 0) {
        for (auto& v : mat) v.fill(val);
    }

    static Matrix id() {
        Matrix m;
        for (int i = 0; i < D; ++i) m[i][i] = 1;
        return m;
    }

    // getter
    std::array<T, D>& operator[](int i) { return mat[i]; }
    std::array<T, D> operator[](int i) const { return mat[i]; }
    typename M::iterator begin() { return mat.begin(); }
    typename M::iterator end() { return mat.end(); }

    // arithmetic
    Matrix operator+(const Matrix& m) const { return Matrix(*this) += m; }
    Matrix operator-(const Matrix& m) const { return Matrix(*this) -= m; }
    Matrix operator*(const Matrix& m) const { return Matrix(*this) *= m; }

    template <class U>
    Matrix pow(U k) {
        Matrix ret = id();
        Matrix a = *this;

        while (k > 0) {
            if (k & 1) ret *= a;
            a *= a;
            k >>= 1;
        }
        return ret;
    }

    // compound assignment
    Matrix& operator+=(const Matrix& m) {
        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                mat[i][j] += m[i][j];
            }
        }
        return *this;
    }
    Matrix& operator-=(const Matrix& m) {
        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                mat[i][j] -= m[i][j];
            }
        }
        return *this;
    }
    Matrix& operator*=(const Matrix& m) {
        M nmat;
        for (auto& v : nmat) v.fill(0);

        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                for (int k = 0; k < D; ++k) {
                    nmat[i][j] += mat[i][k] * m[k][j];
                }
            }
        }
        mat = nmat;
        return *this;
    }

    // arithmetic with vector
    using Vec = Vector<T, D>;
    Vec operator*(const Vec& v) {
        Vec ret;
        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                ret[i] += mat[i][j] * v[j];
            }
        }
        return ret;
    }
};

using mint = ac::modint1000000007;
using Vec = Vector<mint, 3>;
using Mat = Matrix<mint, 3>;

mint calc(int n, int a) {
    Vec v;  // p, q, psum
    v[0] = 1;

    Mat m;
    m[0][0] = a, m[0][1] = mint(a) * a - 1;
    m[1][0] = 1, m[1][1] = a;
    m[2][0] = 1, m[2][2] = 1;

    auto psum = (m.pow(n) * v)[2];
    return psum * 2 - n;
}

void solve() {
    int a, l, r;
    std::cin >> a >> l >> r;

    auto ans = calc(r + 1, a) - calc(l, a);
    std::cout << ans.val() << "\n";
}

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

    int q;
    std::cin >> q;
    while (q--) solve();

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3460
final_rank: 6

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

template <int mod = (int)(1e9 + 7)>
struct ModInt {
  int x;
  constexpr ModInt() : x(0) {}
  constexpr ModInt(int64_t y)
      : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  constexpr ModInt &operator+=(const ModInt &p) noexcept {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &p) noexcept {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &p) noexcept {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt &p) noexcept {
    *this *= p.inverse();
    return *this;
  }
  constexpr ModInt operator-() const { return ModInt(-x); }
  constexpr ModInt operator+(const ModInt &p) const noexcept {
    return ModInt(*this) += p;
  }
  constexpr ModInt operator-(const ModInt &p) const noexcept {
    return ModInt(*this) -= p;
  }
  constexpr ModInt operator*(const ModInt &p) const noexcept {
    return ModInt(*this) *= p;
  }
  constexpr ModInt operator/(const ModInt &p) const noexcept {
    return ModInt(*this) /= p;
  }
  constexpr bool operator==(const ModInt &p) const noexcept { return x == p.x; }
  constexpr bool operator!=(const ModInt &p) const noexcept { return x != p.x; }
  constexpr ModInt inverse() const noexcept {
    int a = x, b = mod, u = 1, v = 0, t = 0;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  constexpr ModInt pow(int64_t n) const {
    ModInt res(1), mul(x);
    while (n) {
      if (n & 1) res *= mul;
      mul *= mul;
      n >>= 1;
    }
    return res;
  }
  friend constexpr ostream &operator<<(ostream &os, const ModInt &p) noexcept {
    return os << p.x;
  }
  friend constexpr istream &operator>>(istream &is, ModInt &a) noexcept {
    int64_t t = 0;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  constexpr int get_mod() { return mod; }
};

template <class T>
struct Matrix {
  vector<vector<T>> A;
  Matrix() {}
  Matrix(size_t m, size_t n) : A(m, vector<T>(n, 0)) {}
  Matrix(size_t n) : A(n, vector<T>(n, 0)) {}
  size_t height() const { return (A.size()); }
  size_t width() const { return (A[0].size()); }
  inline const vector<T> &operator[](int k) const { return (A.at(k)); }
  inline vector<T> &operator[](int k) { return (A.at(k)); }
  static Matrix E(size_t n) {
    Matrix mat(n);
    for (int i = 0; i < n; ++i) mat[i][i] = 1;
    return (mat);
  }
  Matrix &operator+=(const Matrix &B) {
    size_t m = height(), n = width();
    assert(m == B.height() && n == B.width());
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) (*this)[i][j] += B[i][j];
    return (*this);
  }
  Matrix &operator-=(const Matrix &B) {
    size_t m = height(), n = width();
    assert(m == B.height() && n == B.width());
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) (*this)[i][j] -= B[i][j];
    return (*this);
  }
  Matrix &operator*=(const Matrix &B) {
    size_t m = height(), n = B.width(), p = width();
    assert(p == B.height());
    vector<vector<T>> C(m, vector<T>(n, 0));
    for (int i = 0; i < m; ++i)
      for (int k = 0; k < p; ++k) {
        T tmp = (*this)[i][k];
        for (int j = 0; j < n; ++j) C[i][j] += tmp * B[k][j];
      }
    A.swap(C);
    return (*this);
  }
  Matrix &operator^=(long long k) {
    Matrix B = Matrix::E(height());
    while (k) {
      if (k & 1) B *= *this;
      *this *= *this;
      k >>= 1;
    }
    A.swap(B.A);
    return (*this);
  }

  Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }
  Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }
  Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }
  Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); }
  friend ostream &operator<<(ostream &os, Matrix &p) {
    size_t m = p.height(), n = p.width();
    for (int i = 0; i < m; i++) {
      os << "[";
      for (int j = 0; j < n; j++) {
        os << p[i][j] << (j + 1 == n ? "]\n" : ",");
      }
    }
    return (os);
  }
};

int q, a, l, r;

ModInt<> calc(int x);

int main() {
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a >> l >> r;
    cout << calc(r) - calc(l - 1) << endl;
  }
  return 0;
}

ModInt<> calc(int x) {
  if (x < 0) return 0;
  if (a == 1) return x + 1;
  long long b = (long long)a * a - 1;
  Matrix<ModInt<>> mat(4, 4), vec(4, 1);
  mat[0][0] = mat[2][1] = mat[3][3] = vec[0][0] = vec[1][0] = vec[3][0] = 1;
  mat[0][3] = -1;
  mat[0][1] = 2 * a;
  mat[0][2] = 2 * b;
  mat[1][1] = mat[2][2] = a;
  mat[1][2] = b;
  return ((mat ^ x) * vec)[0][0];
}
accuracy: 1
time_ms: 180
memory_kb: 3456
final_rank: 15

code_snippet: /*	author:  Kite_kuma
	created: 2020.09.13 15:07:01 */

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

#pragma region aliases

#define rep(i, n) for(long long i = 0; i < (n); i++)
#define rrep(i, n) for(long long i = (n)-1; i > -1; i--)
#define Rep(i, m, n) for(long long i = (m); i < (n); i++)
#define rRep(i, m, n) for(long long i = (n)-1; i >= (m); i--)
#define REP(i, m, n, p) for(long long i = m; i < n; i += p)
#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bcnt(n) __builtin_popcountll(n)
#define endk endl
#define ednl endl
#define enld endl

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using qll = queue<long long>;
using sll = set<long long>;
using vpll = vector<pair<long long, long long>>;
template <class T = ll>
using V = vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
//昇順pq(小さい方から取り出す)
template <class T = ll>
using pqup = priority_queue<T, vector<T>, greater<T>>;
//降順pq(大きい方から取り出す)
template <class T = ll>
using pqdn = priority_queue<T>;

#pragma endregion

#pragma region constants

long long const limLL = 9223372036854775807;  // POW(2,63)-1 ~ 9.22e18
long long const dekai = 3e16;
const long double pi = acos(-1);
const char el = '\n';
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

const int mod = 1000000007;
// const int mod = 998244353;

#pragma endregion

#pragma region basic_procedure

template <class T>
inline bool isin(T x, T lef, T rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { cout << (f ? "Yes" : "No") << "\n"; }
void No() { cout << "No\n"; }
void YES(bool f = 1) { cout << (f ? "YES" : "NO") << "\n"; }
void NO() { cout << "NO\n"; }
template <class T>
void drop(T answer) {
	cout << answer << "\n";
	exit(0);
}
void err() {
	cout << -1 << "\n";
	exit(0);
}

vector<long long> vin(long long n) {  //整数n個の入力を受け取ってベクトルに突っ込んで返す
	vector<long long> v(n);
	for(long long i = 0; i < n; i++) {
		cin >> v[i];
	}
	return v;
}

//ベクトルの出力(検証済)
// vectorの中身を出力する 答えの出力に利用可能
template <class T>
void vout(vector<T> &v, bool tate = 0) {
	if(v.size() > 0) {
		for(auto it = v.begin(); it < v.end(); it++) {
			cout << *it;
			if(it != v.end() - 1) {
				if(tate)
					cout << endl;
				else
					cout << " ";
			}
		}
	}
	cout << endl;
}

template <class T>
void add(vector<T> &v, T val) {	 //ベクトルの各要素に加算
	for(auto &a : v) a += val;
	return;
}

// vectorの中身を数える map<要素,個数>を返す
template <class T>
map<T, long long> cntv(vector<T> v) {
	map<T, long long> m;
	for(auto &g : v) {
		if(m.count(g))
			m[g]++;
		else
			m[g] = 1;
	}
	return m;
}

//配列圧縮(検証済)
//{1,36,1,3,8,-2,-92}を
//{2, 5,2,3,4, 1,  0}にする
template <class T>
vector<long long> press(vector<T> &v) {
	long long n = v.size();
	vector<long long> w(n);
	map<T, long long> m;
	for(T &p : v) m[p] = 0;
	long long i = 0;
	for(auto &p : m) {
		p.second = i;
		i++;
	}
	for(long long i = 0; i < n; i++) w.at(i) = m[v.at(i)];
	return w;
}

template <class T>
T divup(T a, T b) {
	//端数繰りあがり割り算
	assert(b != 0);
	T x = abs(a);
	T y = abs(b);
	T z = (x + y - 1) / y;
	if((a < 0 && b > 0) || (a > 0 && b < 0))
		return -z;
	else if(a == 0)
		return 0;
	else
		return z;
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

template <class T>
int sgn(T x) {	//符号関数
	if(x < 0) return -1;
	if(x == 0) return 0;
	return 1;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	if(mod == 1) return 0LL;
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

// a * x % mod == __gcd(a,mod)なるxを返す
// a が modの倍数でないことが条件
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		swap(a, b);
		u -= t * v;
		swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

vvll comb(100, vll(100, -1));
long long com(long long n, long long k) {  //普通の二項計数(overflowに注意)
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(comb[n][k] != -1) return comb[n][k];
	ll res;
	if(n - k < k)
		res = com(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = com(n - 1, k - 1) + com(n - 1, k);
	comb[n][k] = res;
	return res;
}

// nCk modを求める
const ll MAX = 5100000;
// この値は求める二項計数の値に応じて変える
// MAX=3*10^7のとき1900msほど、ほぼ比例
// MAX=5*10^6程度ならそれほど気にしなくてよい(300ms程)
long long fac[MAX], finv[MAX], inv[MAX];

void cominit() {
	// テーブルを作る前処理
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(ll i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}
long long commod(ll n, ll k) {	// 二項係数計算
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
long long pmod(ll n, ll k) {  //順列計算
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}
long long hmod(ll n, ll k) {  // nHk計算
	// n個の区別しないoを区別するk個の箱に入れる方法の総数
	//(n+k-1)C(k-1)と等しい
	return commod(n + k - 1, n);
}
#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	INPUT(__VA_ARGS__)

template <class T>
void scan(T &a) {
	cin >> a;
}
template <class T>
void scan(vector<T> &a) {
	for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
template <class T>
inline void print(T x) {
	cout << x << '\n';
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	cerr << ", ";
	view(p.second);
	cerr << ")";
}

template <typename T>
void view(std::set<T> &s) {
	if(s.empty()) {
		cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		cerr << "\n";
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		cerr << ", ";
		view(c.second);
		cerr << ")\n";
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		cerr << "] : ";
		view(t.second);
		cerr << "\n";
	}
	std::cerr << "}";
}

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	view(H);
	cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                           \
	do {                                                     \
		cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                              \
		cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                 \
	do {                                        \
		cerr << __LINE__ << " " << #x << " : "; \
		view(x);                                \
		cerr << "\n";                           \
	} while(0)
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

struct mint {
	long long x;
	mint(long long x = 0) : x((x % mod + mod) % mod) {}
	mint operator-() const { return mint(-x); }
	mint &operator+=(const mint a) {
		if((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint &operator-=(const mint a) {
		if((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint &operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const { return mint(*this) += a; }
	mint operator-(const mint a) const { return mint(*this) -= a; }
	mint operator*(const mint a) const { return mint(*this) *= a; }
	mint pow(long long t) const {
		if(!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if(t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const { return pow(mod - 2); }
	mint &operator/=(const mint a) { return *this *= a.inv(); }
	mint operator/(const mint a) const { return mint(*this) /= a; }
};
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

template <class T, int a, int b = a>
struct matrix {	 // a * b 行列
	vector<vector<T>> mat;
	matrix() : mat(a, vector<T>(b, (T)0)) {}
	matrix(vector<vector<T>> vec) : mat(a, vector<T>(b)) {
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				mat[i][j] = vec[i][j];
			}
		}
	}
	matrix<T, a, b> &operator=(vector<vector<T>> vec) {
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				mat[i][j] = vec[i][j];
			}
		}
		return *this;
	}
	vector<T> &operator[](int row) { return mat[row]; }

	matrix<T, a, b> operator-() const {
		auto m = *this;
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				m.mat[i][j] *= -1;
			}
		}
		return m;
	}

	matrix<T, a, b> &operator+=(const matrix<T, a, b> other) {
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				mat[i][j] += other.mat[i][j];
			}
		}
		return *this;
	}
	matrix<T, a, b> operator+(matrix<T, a, b> other) {
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				other.mat[i][j] += mat[i][j];
			}
		}
		return other;
	}
	matrix<T, a, b> &operator-=(const matrix<T, a, b> other) {
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				mat[i][j] -= other.mat[i][j];
			}
		}
		return *this;
	}
	matrix<T, a, b> operator-(matrix<T, a, b> other) {
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				other.mat[i][j] = mat[i][j] - other.mat[i][j];
			}
		}
		return other;
	}
	template <int c>
	matrix<T, a, c> operator*(const matrix<T, b, c> other) {
		matrix<T, a, c> res;
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < c; j++) {
				for(int k = 0; k < b; k++) {
					res.mat[i][j] += mat[i][k] * other.mat[k][j];
				}
			}
		}
		return res;
	}
	matrix<T, a, b> &operator*=(const matrix<T, b, b> other) {
		matrix<T, a, b> res;
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				for(int k = 0; k < b; k++) {
					res.mat[i][j] += mat[i][k] * other.mat[k][j];
				}
			}
		}
		return *this = res;
	}
	template <class U>
	matrix<T, a, a> pow(U n) {	// n乗
		assert(a == b);
		matrix<T, a, a> ret;
		ret.unit();
		matrix<T, a, a> m = *this;
		while(n > 0) {
			if(n & 1) ret *= m;
			n /= 2;
			m *= m;
		}
		return ret;
	}

	void print() {
		for(int i = 0; i < a; i++) {
			for(int j = 0; j < b; j++) {
				cout << mat[i][j];
				cout << (j == b - 1 ? "\n" : " ");
			}
		}
	}
	void zero() {  // 0行列
		for(int i = 0; i < a; i++)
			for(int j = 0; j < b; j++) mat[i][j] = 0;
	}
	void unit() {  //単位行列
		for(int i = 0; i < a; i++)
			for(int j = 0; j < b; j++) mat[i][j] = (i == j ? 1 : 0);
	}
};

template <class T, int a, int b>
void view(matrix<T, a, b> mat) {
	view(mat.mat);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	srand((unsigned)time(NULL));

	INT(q);
	vector<mint> ans(q);

	rep(i, q) {
		INT(a, l, r);
		if(a == 1) {
			ans[i] = r - l + 1;
			continue;
		}

		// debug(a, l, r);

		matrix<mint, 3, 3> mat;
		mat.mat = {{1, 1, 0}, {0, 2 * a, -1}, {0, 1, 0}};

		auto p = mat.pow(r);

		if(l >= 1) {
			auto rr = mat.pow(l - 1);
			debug(p, rr);
			p -= rr;
		}

		matrix<mint, 3, 1> t;
		t.mat = {{2}, {2 * a}, {2}};
		matrix<mint, 3, 1> g = p * t;

		// debug(mat, g, t);
		// d//ump(p);

		ans[i] = g[0][0] - (r - l + 1);
	}
	vout(ans, 1);

	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3616
final_rank: 20

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

typedef vector<ll> vec;//ll?
typedef vector<vec> mat;

mat mul(mat &A,mat &B){
  mat C(A.size(),vec(B[0].size()));
  for(int i=0;i<A.size();i++){
    for(int k=0;k<B.size();k++){
      for(int j=0;j<B[0].size();j++){
        C[i][j]=(C[i][j]+(A[i][k]*B[k][j])%MOD)%MOD;
      }
    }
  }
  return C;
}

mat pow(mat A,ll n){
  mat B(A.size(),vec(A.size()));
  for(int i=0;i<A.size();i++){
    B[i][i]=1;
  }
  while(n>0){
    if(n&1) B=mul(B,A);
    A=mul(A,A);
    n=(n>>1);
  }
  return B;
}

ll solve(ll R,ll A){
    if(R==0) return 2;
    if(R==1) return (2*A+2)%MOD;

    mat B(3,vec(3,0));
    B[0][0]=2*A%MOD;B[0][1]=MOD-1;B[0][2]=0;
    B[1][0]=1;B[1][1]=0;B[1][2]=0;
    B[2][0]=2*A%MOD;B[2][1]=MOD-1;B[2][2]=1;

    mat C=pow(B,R-1);
    ll ans=(C[2][0]*2%MOD*A%MOD+C[2][1]*2%MOD+C[2][2]*(2*A%MOD+2)%MOD)%MOD;
    return ans;
}

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

  int Q;
  cin>>Q;
  rep(q,Q){
    ll A,L,R;
    cin>>A>>L>>R;

    ll ans=solve(R,A);
    if(L-1>=0) ans=(ans-solve(L-1,A)+MOD)%MOD;
    ans=(ans-(R-L+1)+MOD)%MOD;
    cout<<ans<<"\n";
  }

  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3216
final_rank: 4

code_snippet: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
#include <deque>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
typedef vector<ll> vec;
typedef vector<vec> mat;
const int inf = (int)1e9; 
 
ll gcd(ll a, ll b) {
	if (b > a) {
		ll tmp = b;
		b = a;
		a = tmp;
	}
	if (a%b == 0)return b;
	else return gcd(b, a%b);
}

mat  mul(mat &A,mat &B){
    mat C(A.size(),vec(B[0].size()));
    for(int i=0;i<A.size();i++){
        for(int k=0;k<B.size();k++){
            for(int j=0;j<B[0].size();j++){
                ll t = (A[i][k]*B[k][j])%N;
                C[i][j] = (C[i][j]+t)%N;
                C[i][j] = (C[i][j]+N)%N;
            }
        }
    }
    return C;
}

mat Pow(mat A,ll n){
    mat B(A.size(),vec(A.size()));
    for(int i=0;i<A.size();i++)B[i][i]=1LL;
    while(n>0){
        if(n&1)B = mul(B,A);
        A = mul(A,A);
        n>>=1;
    }
    return B;
}

int main(void){
	int q;
	cin>>q;
	while(q--){
		ll a,l,r;
		cin>>a>>l>>r;
		mat A(2,vec(2));
		A[0][0] = (2*a)%N;
		A[0][1] = (-1+N)%N;
		A[1][0] = 1;
		if(l==r){
			A = Pow(A,r);
			ll ans = (A[1][0]*2*a)%N;
			ans = (ans+A[1][1]*2)%N;
			cout<<(ans-1+N)%N<<endl;
		}
		else{
			if(l==0){
				mat B(2*2,vec(2*2));
				for(int i=0;i<2;i++){
					for(int j=0;j<2;j++){
						B[i][j] = A[i][j];
					}
					B[2+i][i]=1;
					B[2+i][2+i]=1;
				}
				B = Pow(B,r+1);
				ll ans = (B[2+1][0]*2*a)%N;
				ans = (ans+B[2+1][1]*2)%N;
				ans = ans-(r-l+1);
				ans = (ans+N)%N;
				cout<<ans<<endl;
			}
			else{
				mat Br(2*2,vec(2*2)),Bl(2*2,vec(2*2));
				for(int i=0;i<2;i++){
					for(int j=0;j<2;j++){
						Br[i][j] = A[i][j];
						Bl[i][j] = A[i][j];
					}
					Br[2+i][i]=1;
					Br[2+i][2+i]=1;
					Bl[2+i][i]=1;
					Bl[2+i][2+i]=1;
				}
				Br = Pow(Br,r+1);
				Bl = Pow(Bl,l);
				ll ans1 = (Br[3][0]*2*a)%N;
				ans1 = (ans1+Br[3][1]*2)%N;
				ll ans2 = (Bl[3][0]*2*a)%N;
				ans2 = (ans2+Bl[3][1]*2)%N;
				ll ans = (ans1-ans2+N)%N;
				ans = (ans-(r-l+1)+N)%N;
				cout<<ans<<endl;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 3156
final_rank: 19

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat& A, mat& B) {
    mat C(A.size(), vec(B[0].size(), 0));
    for(int i = 0; i < A.size(); ++i) {
        for(int j = 0; j < B[0].size(); ++j) {
            for(int k = 0; k < B.size(); ++k) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j] % mod) % mod;
            }
        }
    }
    return C;
}

mat pow(mat A, ll n) {
    mat B(A.size(), vec(A.size(), 0));
    for(int i = 0; i < A.size(); ++i) {
        B[i][i] = 1LL;
    }
    while(n > 0) {
        if(n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

void solve() {
	int q; cin >> q;
    rep(_, q) {
        int a, l, r; cin >> a >> l >> r;
        if(a == 1) {
            cout << r - l + 1 << endl;
            continue;
        }
        mat A(3, vec(3, 0));
        A[0][0] = (2 * a + 1) % mod, A[0][1] = (- 2 * a - 1 + 2 * mod) % mod, A[0][2] = 1LL, A[1][0] = 1LL, A[2][1] = 1LL;
        ll sl = 0, sr = 0;
        if(l >= 2) {
            mat B = pow(A, l - 2);
            sl = (B[0][0] * ((2 * a + 2) % mod) % mod + B[0][1] * 2LL % mod) % mod;
        } else {
            if(l == 1) sl = 2;
            if(l == 0) sl = 0;
        }
        if(r + 1 >= 2) {
            mat B = pow(A, r + 1 - 2);
            sr = (B[0][0] * ((2 * a + 2) % mod) % mod + B[0][1] * 2LL % mod) % mod;
        } else {
            sr = 2;
        }
        cout << ((sr - sl + mod) % mod - (r - l + 1) + mod) % mod << endl;
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 3212
final_rank: 5

code_snippet: #pragma GCC target("avx2,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
//#include <atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//using i128 = __int128_t;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
constexpr char ln = '\n';
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
inline int topbit(int x) {return x == 0 ? -1 : 31-__builtin_clz(x);}
inline int topbit(long long x) {return x == 0 ? -1 : 63-__builtin_clzll(x);}
inline int botbit(int x) {return x == 0 ? 32 : __builtin_ctz(x);}
inline int botbit(long long x) {return x == 0 ? 64 : __builtin_ctzll(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
inline int kthbit(long long x, int k) {return (x>>k)&1;}
inline void print() {cout << "\n";}
template<class T>
inline void print(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cout << *itr << " ";
    print();
}
template<class T, class... Args>
inline void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
#ifdef MINATO_LOCAL
#define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
inline void debug() {cerr << endl;}
template<class T>
inline void debug(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cerr << *itr << " ";
    debug();
}
template<class T, class... Args>
inline void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
#else
#define dump(x) void(0)
inline void debug() {}
template<class T> inline void debug(const vector<T> &v) {}
template<class T, class... Args> inline void debug(const T &x, const Args &... args) {}
#endif
struct Fast_ios {Fast_ios() {cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20);};} fast_ios;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template<typename T, int sz>
struct Matrix {
   	array<array<T, sz>, sz> A;
	int N;

   	Matrix() : N(sz) {}
   	Matrix(int N) : N(N) {}

	const array<T, sz> &operator[](int k) const {return A[k];}
  	array<T, sz> &operator[](int k) {return A[k];}

  	static Matrix I(int N) {
    	Matrix<T, sz> mat(N);
    	for(int i = 0; i < N; i++) mat[i][i] = 1;
    	return mat;
  	}

  	Matrix &operator+=(const Matrix &B) {
    	for(int i = 0; i < N; i++) {
      	    for(int j = 0; j < N; j++) {
        	    (*this)[i][j] += B[i][j];
            }
        }
    	return *this;
  	}

  	Matrix &operator-=(const Matrix &B) {
    	for(int i = 0; i < N; i++) {
      		for(int j = 0; j < N; j++) {
				(*this)[i][j] -= B[i][j];
			}
		}
    	return *this;
  	}

    Matrix &operator*=(const Matrix &B) {
        Matrix<T, sz> C(N);
        for(int i = 0; i < N; i++) {
            for(int k = 0; k < N; k++) {
                for(int j = 0; j < N; j++) {
                    C[i][j] += (*this)[i][k] * B[k][j];
                }
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                swap(A[i][j],C.A[i][j]);
            }
        }
        return *this;
    }

    Matrix &operator^=(long long k) {
        Matrix<T, sz> B = Matrix<T, sz>::I(N);
        while(k) {
            if(k & 1) B *= *this;
            *this *= *this;
            k >>= 1;
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                swap(A[i][j],B.A[i][j]);
            }
        }
        return *this;
    }

    Matrix operator+(const Matrix &B) const {return (Matrix(*this) += B);}
    Matrix operator-(const Matrix &B) const {return (Matrix(*this) -= B);}
    Matrix operator*(const Matrix &B) const {return (Matrix(*this) *= B);}
    Matrix operator^(const long long k) const {return (Matrix(*this) ^= k);}
};

template <uint_fast64_t Modulus> 
struct ModInt {
    using u64 = uint_fast64_t;

    u64 a;

    constexpr ModInt(const long long x = 0) noexcept : a(x >= 0 ? x % Modulus : (Modulus - (-x) % Modulus) % Modulus) {}
    constexpr u64 &value() noexcept {return a;}
    constexpr const u64 &value() const noexcept {return a;}
    constexpr ModInt operator+(const ModInt rhs) const noexcept {return ModInt(*this) += rhs;}
    constexpr ModInt operator-(const ModInt rhs) const noexcept {return ModInt(*this) -= rhs;}
    constexpr ModInt operator*(const ModInt rhs) const noexcept {return ModInt(*this) *= rhs;}
    constexpr ModInt operator/(const ModInt rhs) const noexcept {return ModInt(*this) /= rhs;}
    constexpr ModInt operator^(const long long rhs) const noexcept {return ModInt(*this) ^= rhs;}
    constexpr bool operator==(const ModInt &rhs) const noexcept {return a == rhs.a;}
    constexpr bool operator!=(const ModInt &rhs) const noexcept {return a != rhs.a;}
    constexpr ModInt &operator+=(const ModInt rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus) {
            a -= Modulus;
        }
        return *this;
    }
    constexpr ModInt &operator-=(const ModInt rhs) noexcept {
        if (a < rhs.a) {
            a += Modulus;
        }
        a -= rhs.a;
        return *this;
    }
    constexpr ModInt &operator*=(const ModInt rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr ModInt &operator/=(ModInt rhs) noexcept {
        u64 exp = Modulus - 2;
        while (exp) {
            if (exp&1) *this *= rhs;
            exp >>= 1;
            rhs *= rhs;
        }
        return *this;
    }
    constexpr ModInt &operator^=(long long exp) noexcept {
        ModInt rhs = a;
        a = 1;
        while (exp) {
            if (exp&1) *this *= rhs;
            exp >>= 1;
            rhs *= rhs;
        }
        return *this;
    }

    friend ostream &operator<<(ostream& os, const ModInt& rhs) noexcept {return os << rhs.a;}
    friend istream &operator>>(istream& is, ModInt& rhs) noexcept {long long a; is >> a; rhs = a; return is;}
};

constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;

using mint = ModInt<MOD>;

int main() {
    Matrix<mint, 4> mat(4);
    int Q; cin >> Q;
    while(Q--) {
        ll a,l,r; cin >> a >> l >> r;
        if (a==1) {
            cout << r-l+1 << ln;
            continue;
        }
        mint root = a*a-1;
        rep(i,4) rep(j,4) mat[i][j] = 0;
        mat[0][0] = mat[2][1] = mat[3][3] = 1;
        mat[0][1] = a*2;
        mat[0][2] = root*2;
        mat[0][3] = -1;
        mat[1][1] = mat[2][2] = a;
        mat[1][2] = root;
        mat ^= r;
        mint ans = mat[0][0]+mat[0][1]+mat[0][3];
        //dump(ans);
        if (l) {
            rep(i,4) rep(j,4) mat[i][j] = 0;
            mat[0][0] = mat[2][1] = mat[3][3] = 1;
            mat[0][1] = a*2;
            mat[0][2] = root*2;
            mat[0][3] = -1;
            mat[1][1] = mat[2][2] = a;
            mat[1][2] = root;
            mat ^= (l-1);
            ans -= mat[0][0]+mat[0][1]+mat[0][3];
        }

        cout << ans << ln;
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3452
final_rank: 8

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <int MOD>
struct MInt {
  unsigned val;
  MInt(): val(0) {}
  MInt(long long x) : val(x >= 0 ? x % MOD : x % MOD + MOD) {}
  static int get_mod() { return MOD; }
  static void set_mod(int divisor) { assert(divisor == MOD); }
  MInt pow(long long exponent) const {
    MInt tmp = *this, res = 1;
    while (exponent > 0) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
      exponent >>= 1;
    }
    return res;
  }
  MInt &operator+=(const MInt &x) { if((val += x.val) >= MOD) val -= MOD; return *this; }
  MInt &operator-=(const MInt &x) { if((val += MOD - x.val) >= MOD) val -= MOD; return *this; }
  MInt &operator*=(const MInt &x) { val = static_cast<unsigned long long>(val) * x.val % MOD; return *this; }
  MInt &operator/=(const MInt &x) {
    // assert(std::__gcd(static_cast<int>(x.val), MOD) == 1);
    unsigned a = x.val, b = MOD; int u = 1, v = 0;
    while (b) {
      unsigned tmp = a / b;
      std::swap(a -= tmp * b, b);
      std::swap(u -= tmp * v, v);
    }
    return *this *= u;
  }
  bool operator==(const MInt &x) const { return val == x.val; }
  bool operator!=(const MInt &x) const { return val != x.val; }
  bool operator<(const MInt &x) const { return val < x.val; }
  bool operator<=(const MInt &x) const { return val <= x.val; }
  bool operator>(const MInt &x) const { return val > x.val; }
  bool operator>=(const MInt &x) const { return val >= x.val; }
  MInt &operator++() { if (++val == MOD) val = 0; return *this; }
  MInt operator++(int) { MInt res = *this; ++*this; return res; }
  MInt &operator--() { val = (val == 0 ? MOD : val) - 1; return *this; }
  MInt operator--(int) { MInt res = *this; --*this; return res; }
  MInt operator+() const { return *this; }
  MInt operator-() const { return MInt(val ? MOD - val : 0); }
  MInt operator+(const MInt &x) const { return MInt(*this) += x; }
  MInt operator-(const MInt &x) const { return MInt(*this) -= x; }
  MInt operator*(const MInt &x) const { return MInt(*this) *= x; }
  MInt operator/(const MInt &x) const { return MInt(*this) /= x; }
  friend std::ostream &operator<<(std::ostream &os, const MInt &x) { return os << x.val; }
  friend std::istream &operator>>(std::istream &is, MInt &x) { long long val; is >> val; x = MInt(val); return is; }
};
namespace std { template <int MOD> MInt<MOD> abs(const MInt<MOD> &x) { return x; } }
template <int MOD>
struct Combinatorics {
  using ModInt = MInt<MOD>;
  int val;  // "val!" and "mod" must be disjoint.
  std::vector<ModInt> fact, fact_inv, inv;
  Combinatorics(int val = 10000000) : val(val), fact(val + 1), fact_inv(val + 1), inv(val + 1) {
    fact[0] = 1;
    for (int i = 1; i <= val; ++i) fact[i] = fact[i - 1] * i;
    fact_inv[val] = ModInt(1) / fact[val];
    for (int i = val; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i;
    for (int i = 1; i <= val; ++i) inv[i] = fact[i - 1] * fact_inv[i];
  }
  ModInt nCk(int n, int k) const {
    if (n < 0 || n < k || k < 0) return 0;
    assert(n <= val && k <= val);
    return fact[n] * fact_inv[k] * fact_inv[n - k];
  }
  ModInt nPk(int n, int k) const {
    if (n < 0 || n < k || k < 0) return 0;
    assert(n <= val);
    return fact[n] * fact_inv[n - k];
  }
  ModInt nHk(int n, int k) const {
    if (n < 0 || k < 0) return 0;
    return k == 0 ? 1 : nCk(n + k - 1, k);
  }
};
using ModInt = MInt<MOD>;

template <typename T>
struct Matrix {
  Matrix(int m, int n, T val = 0) : dat(m, std::vector<T>(n, val)) {}

  int height() const { return dat.size(); }

  int width() const { return dat.front().size(); }

  Matrix pow(long long exponent) const {
    int n = height();
    Matrix<T> tmp = *this, res(n, n, 0);
    for (int i = 0; i < n; ++i) res[i][i] = 1;
    while (exponent > 0) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
      exponent >>= 1;
    }
    return res;
  }

  inline const std::vector<T> &operator[](const int idx) const { return dat[idx]; }
  inline std::vector<T> &operator[](const int idx) { return dat[idx]; }

  Matrix &operator=(const Matrix &x) {
    int m = x.height(), n = x.width();
    dat.resize(m, std::vector<T>(n));
    for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) dat[i][j] = x[i][j];
    return *this;
  }

  Matrix &operator+=(const Matrix &x) {
    int m = height(), n = width();
    for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) dat[i][j] += x[i][j];
    return *this;
  }

  Matrix &operator-=(const Matrix &x) {
    int m = height(), n = width();
    for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) dat[i][j] -= x[i][j];
    return *this;
  }

  Matrix &operator*=(const Matrix &x) {
    int m = height(), n = x.width(), l = width();
    std::vector<std::vector<T>> res(m, std::vector<T>(n, 0));
    for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) {
      for (int k = 0; k < l; ++k) res[i][j] += dat[i][k] * x[k][j];
    }
    std::swap(dat, res);
    return *this;
  }

  Matrix operator+(const Matrix &x) const { return Matrix(*this) += x; }

  Matrix operator-(const Matrix &x) const { return Matrix(*this) -= x; }

  Matrix operator*(const Matrix &x) const { return Matrix(*this) *= x; }

private:
  std::vector<std::vector<T>> dat;
};

ModInt solve(int a, int r) {
  if (r == -1) return 0;
  if (r == 0) return 2;
  Matrix<ModInt> m(3, 3), init(3, 1);
  m[0][0] = 1; m[0][1] = a * 2; m[0][2] = -1;
               m[1][1] = a * 2; m[1][2] = -1;
               m[2][1] = 1;
  init[0][0] = a * 2 + 2;
  init[1][0] = a * 2;
  init[2][0] = 2;
  return (m.pow(r - 1) * init)[0][0];
}

int main() {
  int q; cin >> q;
  while (q--) {
    int a, l, r; cin >> a >> l >> r;
    cout << solve(a, r) - solve(a, l - 1) - (r - l + 1) << '\n';
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3452
final_rank: 10

code_snippet: #include<bits/stdc++.h>

int main(){
    using namespace std;
    using f_1e9_7_va = pair<unsigned long, unsigned long>;
    constexpr unsigned long MOD = 1000000007;
    constexpr unsigned long buf_size = 3UL << 18UL;
    char buffer[buf_size];
    fread(buffer, 1, buf_size, stdin);
    unsigned long tmp_buf = 0;
    const auto& read = [&tmp_buf, &buffer]{
        const auto& M_reread_from_stdin = [&]{
            ptrdiff_t len = buf_size - tmp_buf;
            if (len > tmp_buf) return;
            memcpy(buffer, buffer + tmp_buf, len);
            char* tmp = buffer + len;
            fread(tmp, 1, buf_size-len, stdin);
            tmp_buf = 0;
        };
        unsigned long ret;
        from_chars_result tmp{};
        do{
            if (__builtin_expect(buf_size <= tmp_buf + 32, 0))
                M_reread_from_stdin();
            tmp = from_chars(begin(buffer) + tmp_buf, end(buffer), ret);
            tmp_buf = tmp.ptr - buffer + 1;
        }while(tmp.ec != errc{});
        return ret;
    };
    const auto& scan = [scan_impl = [&read](auto& x) -> decltype(auto) { return x = read(); }](auto&...args){ tuple<decltype(args)...>{scan_impl(args)...}; };
    const unsigned long Q{read()};
    const auto& modinv = [](unsigned long a, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD}, n{MOD - 2};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };
    for(unsigned long i{0}, a, l, r; i < Q; ++i)cout << [&modinv, &scan, &a, &l, &r]{
        scan(a, l, r);
        ++r;
        if(a == 1)return r - l;
        const auto& mul = [&a](const f_1e9_7_va& x, const f_1e9_7_va& y) -> f_1e9_7_va {
            f_1e9_7_va ret{x.first * y.first + MOD - x.second * y.second % MOD, x.first * y.second + x.second * y.first + 2 * a * x.second % MOD * y.second};
            ret.first %= MOD;
            ret.second %= MOD;
            return ret;
        };
        const auto& modpow = [&mul](const f_1e9_7_va& a, unsigned long n, const f_1e9_7_va& b = {1, 0}) -> f_1e9_7_va {
            f_1e9_7_va ret{b}, al{a};
            while(n){
                if(n & 1)ret = mul(ret, al);
                al = mul(al, al);
                n >>= 1;
            }
            return ret;
        };
        const auto& subtract = [](const f_1e9_7_va& a, const f_1e9_7_va& b) -> f_1e9_7_va {
            return {(a.first + MOD - b.first) % MOD, (a.second + MOD - b.second) % MOD};
        };
        unsigned long k{modinv(2 * a - 2)}, m{(2 * MOD - 1 - k) % MOD};
        f_1e9_7_va tmp{m, k}, x{modpow({0, 1}, r, tmp)}, y{modpow({0, 1}, l, tmp)}, z{subtract(x, y)};
        return (2 * (z.first + a * z.second) + l + MOD - r) % MOD;
    }() << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3716
final_rank: 16

code_snippet: #include<bits/stdc++.h>

int main(){
    using namespace std;
    using f_1e9_7_va = pair<unsigned long, unsigned long>;
    constexpr unsigned long MOD = 1000000007;
    constexpr unsigned long buf_size = 1UL << 20UL;
    char buffer[buf_size];
    fread(buffer, 1, buf_size, stdin);
    unsigned long tmp_buf = 0;
    const auto& read = [&tmp_buf, &buffer]{
        const auto& M_reread_from_stdin = [&]{
            ptrdiff_t len = buf_size - tmp_buf;
            if (len > tmp_buf) return;
            memcpy(buffer, buffer + tmp_buf, len);
            char* tmp = buffer + len;
            fread(tmp, 1, buf_size-len, stdin);
            tmp_buf = 0;
        };
        unsigned long ret;
        from_chars_result tmp{};
        do{
            if (__builtin_expect(buf_size <= tmp_buf + 32, 0))
                M_reread_from_stdin();
            tmp = from_chars(begin(buffer) + tmp_buf, end(buffer), ret);
            tmp_buf = tmp.ptr - buffer + 1;
        }while(tmp.ec != errc{});
        return ret;
    };
    const auto& scan = [scan_impl = [&read](auto& x) -> decltype(auto) { return x = read(); }](auto&...args){ tuple<decltype(args)...>{scan_impl(args)...}; };
    const unsigned long Q{read()};
    const auto& modinv = [](unsigned long a, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD}, n{MOD - 2};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };
    for(unsigned long i{0}, a, l, r; i < Q; ++i)cout << [&modinv, &scan, &a, &l, &r]{
        scan(a, l, r);
        ++r;
        if(a == 1)return r - l;
        const auto& mul = [&a](const f_1e9_7_va& x, const f_1e9_7_va& y) -> f_1e9_7_va {
            f_1e9_7_va ret{x.first * y.first + MOD - x.second * y.second % MOD, x.first * y.second + x.second * y.first + 2 * a * x.second % MOD * y.second};
            ret.first %= MOD;
            ret.second %= MOD;
            return ret;
        };
        const auto& modpow = [&mul](const f_1e9_7_va& a, unsigned long n, const f_1e9_7_va& b = {1, 0}) -> f_1e9_7_va {
            f_1e9_7_va ret{b}, al{a};
            while(n){
                if(n & 1)ret = mul(ret, al);
                al = mul(al, al);
                n >>= 1;
            }
            return ret;
        };
        const auto& subtract = [](const f_1e9_7_va& a, const f_1e9_7_va& b) -> f_1e9_7_va {
            return {(a.first + MOD - b.first) % MOD, (a.second + MOD - b.second) % MOD};
        };
        unsigned long k{modinv(2 * a - 2)}, m{(2 * MOD - 1 - k) % MOD};
        f_1e9_7_va tmp{m, k}, x{modpow({0, 1}, r, tmp)}, y{modpow({0, 1}, l, tmp)}, z{subtract(x, y)};
        return (2 * (z.first + a * z.second) + l + MOD - r) % MOD;
    }() << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3648
final_rank: 11

code_snippet: #include<bits/stdc++.h>

int main(){
    using namespace std;
    using f_1e9_7_va = pair<unsigned long, unsigned long>;
    constexpr unsigned long MOD = 1000000007;
    constexpr unsigned long buf_size = 1UL << 17UL;
    char buffer[buf_size];
    fread(buffer, 1, buf_size, stdin);
    unsigned long tmp_buf = 0;
    const auto& read = [&tmp_buf, &buffer]{
        const auto& M_reread_from_stdin = [&]{
            ptrdiff_t len = buf_size - tmp_buf;
            if (len > tmp_buf) return;
            memcpy(buffer, buffer + tmp_buf, len);
            char* tmp = buffer + len;
            fread(tmp, 1, buf_size-len, stdin);
            tmp_buf = 0;
        };
        unsigned long ret;
        from_chars_result tmp{};
        do{
            if (__builtin_expect(buf_size <= tmp_buf + 20, 0))
                M_reread_from_stdin();
            tmp = from_chars(begin(buffer) + tmp_buf, end(buffer), ret);
            tmp_buf = tmp.ptr - buffer + 1;
        }while(tmp.ec != errc{});
        return ret;
    };
    const auto& scan = [scan_impl = [&read](auto& x) -> decltype(auto) { return x = read(); }](auto&...args){ tuple<decltype(args)...>{scan_impl(args)...}; };
    const unsigned long Q{read()};
    const auto& modinv = [](unsigned long a, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD}, n{MOD - 2};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };
    for(unsigned long i{0}, a, l, r; i < Q; ++i)cout << [&modinv, &scan, &a, &l, &r]{
        scan(a, l, r);
        ++r;
        if(a == 1)return r - l;
        const auto& mul = [&a](const f_1e9_7_va& x, const f_1e9_7_va& y) -> f_1e9_7_va {
            f_1e9_7_va ret{x.first * y.first + MOD - x.second * y.second % MOD, x.first * y.second + x.second * y.first + 2 * a * x.second % MOD * y.second};
            ret.first %= MOD;
            ret.second %= MOD;
            return ret;
        };
        const auto& modpow = [&mul](const f_1e9_7_va& a, unsigned long n, const f_1e9_7_va& b = {1, 0}) -> f_1e9_7_va {
            f_1e9_7_va ret{b}, al{a};
            while(n){
                if(n & 1)ret = mul(ret, al);
                al = mul(al, al);
                n >>= 1;
            }
            return ret;
        };
        const auto& subtract = [](const f_1e9_7_va& a, const f_1e9_7_va& b) -> f_1e9_7_va {
            return {(a.first + MOD - b.first) % MOD, (a.second + MOD - b.second) % MOD};
        };
        unsigned long k{modinv(2 * a - 2)}, m{(2 * MOD - 1 - k) % MOD};
        f_1e9_7_va tmp{m, k}, x{modpow({0, 1}, r, tmp)}, y{modpow({0, 1}, l, tmp)}, z{subtract(x, y)};
        return (2 * (z.first + a * z.second) + l + MOD - r) % MOD;
    }() << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3540
final_rank: 9

code_snippet: #include<bits/stdc++.h>

int main(){
    using namespace std;
    using f_1e9_7_va = pair<unsigned long, unsigned long>;
    constexpr unsigned long MOD = 1000000007;
    char buffer[1UL << 19UL];
    fread(buffer, 1, size(buffer), stdin);
    unsigned long tmp_buf = 0;
    const auto& read = [&tmp_buf, &buffer]{
        unsigned long ret;
        from_chars_result tmp{};
        do{
            tmp = from_chars(begin(buffer) + tmp_buf, end(buffer), ret);
            tmp_buf = tmp.ptr - buffer + 1;
        }while(tmp.ec != errc{});
        return ret;
    };
    const auto& scan = [scan_impl = [&read](auto& x) -> decltype(auto) {
        return x = read();
    }](auto&...args){
        tuple<decltype(args)...>{scan_impl(args)...};
    };
    const unsigned long Q{read()};
    const auto& modinv = [](unsigned long a, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD}, n{MOD - 2};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };
    for(unsigned long i{0}, a, l, r; i < Q; ++i)cout << [&modinv, &scan, &a, &l, &r]{
        scan(a, l, r);
        ++r;
        if(a == 1)return r - l;
        const auto& mul = [&a](const f_1e9_7_va& x, const f_1e9_7_va& y) -> f_1e9_7_va {
            f_1e9_7_va ret{x.first * y.first + MOD - x.second * y.second % MOD, x.first * y.second + x.second * y.first + 2 * a * x.second % MOD * y.second};
            ret.first %= MOD;
            ret.second %= MOD;
            return ret;
        };
        const auto& modpow = [&mul](const f_1e9_7_va& a, unsigned long n, const f_1e9_7_va& b = {1, 0}) -> f_1e9_7_va {
            f_1e9_7_va ret{b}, al{a};
            while(n){
                if(n & 1)ret = mul(ret, al);
                al = mul(al, al);
                n >>= 1;
            }
            return ret;
        };
        const auto& subtract = [](const f_1e9_7_va& a, const f_1e9_7_va& b) -> f_1e9_7_va {
            return {(a.first + MOD - b.first) % MOD, (a.second + MOD - b.second) % MOD};
        };
        unsigned long k{modinv(2 * a - 2)}, m{(2 * MOD - 1 - k) % MOD};
        f_1e9_7_va tmp{m, k}, x{modpow({0, 1}, r, tmp)}, y{modpow({0, 1}, l, tmp)}, z{subtract(x, y)};
        return (2 * (z.first + a * z.second) + l + MOD - r) % MOD;
    }() << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3716
final_rank: 16

code_snippet: #include<bits/stdc++.h>

int main(){
    using namespace std;
    using f_1e9_7_va = pair<unsigned long, unsigned long>;
    constexpr unsigned long MOD = 1000000007;
    char buffer[1UL << 19UL];
    fread(buffer, 1, size(buffer), stdin);
    unsigned long tmp_buf = 0;
    const auto& read = [&tmp_buf, &buffer]{
        unsigned long ret;
        from_chars_result tmp{};
        do{
            tmp = from_chars(begin(buffer) + tmp_buf, end(buffer), ret);
            tmp_buf = tmp.ptr - buffer + 1;
        }while(tmp.ec != errc{});
        return ret;
    };
    const auto& scan = [scan_impl = [&read](auto& x) -> auto {
        return x = read();
    }](auto&...args){
        [](auto... args) -> void {}(scan_impl(args)...);
    };
    const unsigned long Q{read()};
    const auto& modinv = [](unsigned long a, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD}, n{MOD - 2};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };
    for(unsigned long i{0}, a, l, r; i < Q; ++i)cout << [&modinv, &scan, &a, &l, &r]{
        scan(r, l, a);
        ++r;
        if(a == 1)return r - l;
        const auto& mul = [&a](const f_1e9_7_va& x, const f_1e9_7_va& y) -> f_1e9_7_va {
            f_1e9_7_va ret{x.first * y.first + MOD - x.second * y.second % MOD, x.first * y.second + x.second * y.first + 2 * a * x.second % MOD * y.second};
            ret.first %= MOD;
            ret.second %= MOD;
            return ret;
        };
        const auto& modpow = [&mul](const f_1e9_7_va& a, unsigned long n, const f_1e9_7_va& b = {1, 0}) -> f_1e9_7_va {
            f_1e9_7_va ret{b}, al{a};
            while(n){
                if(n & 1)ret = mul(ret, al);
                al = mul(al, al);
                n >>= 1;
            }
            return ret;
        };
        const auto& subtract = [](const f_1e9_7_va& a, const f_1e9_7_va& b) -> f_1e9_7_va {
            return {(a.first + MOD - b.first) % MOD, (a.second + MOD - b.second) % MOD};
        };
        unsigned long k{modinv(2 * a - 2)}, m{(2 * MOD - 1 - k) % MOD};
        f_1e9_7_va tmp{m, k}, x{modpow({0, 1}, r, tmp)}, y{modpow({0, 1}, l, tmp)}, z{subtract(x, y)};
        return (2 * (z.first + a * z.second) + l + MOD - r) % MOD;
    }() << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3720
final_rank: 18

code_snippet: #pragma region Macros
#pragma GCC optimize("O3")
#pragma GCC target("avx2,avx")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep2(i, a, b) for(ll i = a; i <= b; ++i)
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define rep3(i, a, b) for(ll i = a; i >= b; --i)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
using namespace std;
string YES[2] = {"NO", "YES"};
string Yes[2] = {"No", "Yes"};
string yes[2] = {"no", "yes"};
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define si(c) (int)(c).size()
#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &... tail) {
    scan(head);
    IN(tail...);
}
template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
vi iota(int n) {
    vi a(n);
    iota(all(a), 0);
    return a;
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    sort(all(y));
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
int popcount(ll x) { return __builtin_popcountll(x); }
struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(15);
    }
} setup_io;
int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}

template <typename T> struct edge {
    int from, to;
    T cost;
    int id;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;

using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
    Tree res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        cin >> a >> b;
        a -= margin, b -= margin;
        res[a].emplace_back(b);
        if(!directed) res[b].emplace_back(a);
    }
    return move(res);
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
    Wgraph<T> res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        T c;
        cin >> a >> b >> c;
        a -= margin, b -= margin;
        res[a].emplace_back(b, c);
        if(!directed) res[b].emplace_back(a, c);
    }
    return move(res);
}

#define i128 __int128_t
#define ull unsigned long long int
#define TEST                                                                                                                                                   \
    INT(testcases);                                                                                                                                            \
    while(testcases--)
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    for(auto &e : v) cout << e << " ";
    cout << endl;
    return os;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    cout << "(" << p.fi << ", " << p.se << ")";
    return os;
}
template <class S, class T> string to_string(pair<S, T> p) { return "(" + to_string(p.first) + "," + to_string(p.second) + ")"; }
template <class A> string to_string(A v) {
    if(v.empty()) return "{}";
    string ret = "{";
    for(auto &x : v) ret += to_string(x) + ",";
    ret.back() = '}';
    return ret;
}

void dump() { cerr << endl; }
template <class Head, class... Tail> void dump(Head head, Tail... tail) {
    cerr << to_string(head) << " ";
    dump(tail...);
}
#define endl '\n'
#ifdef _LOCAL
#undef endl
#define debug(x)                                                                                                                                               \
    cout << #x << ": ";                                                                                                                                        \
    dump(x)
#else
#define debug(x)
#endif
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); }
// ll rndll(ll n) { return uniform_int_distribution<ll>(0, n - 1)(rng); }

template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
#pragma endregion
template <class T> struct Matrix {
    vector<vector<T>> A;

    Matrix() {}

    Matrix(size_t n, size_t m) : A(n, vector<T>(m, 0)) {}

    Matrix(size_t n) : A(n, vector<T>(n, 0)){};

    size_t height() const { return (A.size()); }

    size_t width() const { return (A[0].size()); }

    inline const vector<T> &operator[](int k) const { return (A.at(k)); }

    inline vector<T> &operator[](int k) { return (A.at(k)); }

    static Matrix I(size_t n) {
        Matrix mat(n);
        for(int i = 0; i < n; i++) mat[i][i] = 1;
        return (mat);
    }

    Matrix &operator+=(const Matrix &B) {
        size_t n = height(), m = width();
        assert(n == B.height() && m == B.width());
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++) (*this)[i][j] += B[i][j];
        return (*this);
    }

    Matrix &operator-=(const Matrix &B) {
        size_t n = height(), m = width();
        assert(n == B.height() && m == B.width());
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++) (*this)[i][j] -= B[i][j];
        return (*this);
    }

    Matrix &operator*=(const Matrix &B) {
        size_t n = height(), m = B.width(), p = width();
        assert(p == B.height());
        vector<vector<T>> C(n, vector<T>(m, 0));
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                for(int k = 0; k < p; k++) C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
        A.swap(C);
        return (*this);
    }

    Matrix &operator^=(long long k) {
        Matrix B = Matrix::I(height());
        while(k > 0) {
            if(k & 1) B *= *this;
            *this *= *this;
            k >>= 1LL;
        }
        A.swap(B.A);
        return (*this);
    }

    Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }

    Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }

    Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }

    Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); }

    friend ostream &operator<<(ostream &os, Matrix &p) {
        size_t n = p.height(), m = p.width();
        for(int i = 0; i < n; i++) {
            os << "[";
            for(int j = 0; j < m; j++) { os << p[i][j] << (j + 1 == m ? "]\n" : ","); }
        }
        return (os);
    }

    T determinant() {
        Matrix B(*this);
        assert(width() == height());
        T ret = 1;
        for(int i = 0; i < width(); i++) {
            int idx = -1;
            for(int j = i; j < width(); j++) {
                if(B[j][i] != 0) idx = j;
            }
            if(idx == -1) return (0);
            if(i != idx) {
                ret *= -1;
                swap(B[i], B[idx]);
            }
            ret *= B[i][i];
            T vv = B[i][i];
            for(int j = 0; j < width(); j++) { B[i][j] /= vv; }
            for(int j = i + 1; j < width(); j++) {
                T a = B[j][i];
                for(int k = 0; k < width(); k++) { B[j][k] -= B[i][k] * a; }
            }
        }
        return (ret);
    }
};
namespace modular {
constexpr ll MOD = 1000000007;
const int MAXN = 1100000;
template <ll Modulus> class modint;
#define mint modint<MOD>
#define vmint vector<mint>
vector<mint> Inv;
mint inv(int x);
template <ll Modulus> class modint {

  public:
    static constexpr int mod() { return Modulus; }
    ll a;

    constexpr modint(const ll x = 0) noexcept : a(((x % Modulus) + Modulus) % Modulus) {}
    constexpr ll &value() noexcept { return a; }
    constexpr const ll &value() const noexcept { return a; }
    constexpr modint operator-() const noexcept { return modint() - *this; }
    constexpr modint operator+() const noexcept { return *this; }
    constexpr modint &operator++() noexcept {
        if(++a == MOD) a = 0;
        return *this;
    }
    constexpr modint &operator--() noexcept {
        if(!a) a = MOD;
        a--;
        return *this;
    }
    constexpr modint operator++(int) {
        modint res = *this;
        ++*this;
        return res;
    }
    constexpr modint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    constexpr modint &operator+=(const modint rhs) noexcept {
        a += rhs.a;
        if(a >= Modulus) { a -= Modulus; }
        return *this;
    }
    constexpr modint &operator-=(const modint rhs) noexcept {
        if(a < rhs.a) { a += Modulus; }
        a -= rhs.a;
        return *this;
    }
    constexpr modint &operator*=(const modint rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr modint &operator/=(const modint rhs) noexcept {
        a = a * (modular::inv(rhs.a)).a % Modulus;
        return *this;
    }
    constexpr modint pow(long long n) const noexcept {
        assert(n >= 0);
        modint x = *this, r = 1;
        while(n) {
            if(n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    constexpr modint inv() const noexcept { return pow(Modulus - 2); }
    constexpr friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs) += modint(rhs); }
    constexpr friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs) -= modint(rhs); }
    constexpr friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs) *= modint(rhs); }
    constexpr friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs) /= modint(rhs); }
    constexpr friend modint operator==(const modint &lhs, const modint &rhs) { return lhs.a == rhs.a; }
    constexpr friend modint operator!=(const modint &lhs, const modint &rhs) { return lhs.a != rhs.a; }
    // constexpr friend modint operator^=(const modint &lhs, const modint &rhs) { return modint(lhs) ^= modint(rhs); }
};
vmint Prd{1, 1}, Invprd{1, 1};
mint inv(int n) {
    if(Inv.empty()) Inv.emplace_back(0), Inv.emplace_back(1);
    if(Inv.size() > n)
        return Inv[n];
    else {
        for(int i = Inv.size(); i <= n; ++i) Inv.emplace_back(Inv[MOD % i] * (-MOD / i));
        return Inv[n];
    }
}
mint prd(int n) {
    if(Prd.size() > n)
        return Prd[n];
    else
        for(int i = Prd.size(); i <= n; ++i) Prd.emplace_back(Prd[i - 1] * i);
    return Prd[n];
}
mint invprd(int n) {
    if(Invprd.size() > n)
        return Invprd[n];
    else
        for(int i = Invprd.size(); i <= n; ++i) Invprd.emplace_back(Invprd[i - 1] * inv(i));
    return Invprd[n];
}
mint modpow(ll a, ll n) { return mint(a).pow(n); }
mint inv(mint a) { return inv(a.a); }
mint invprd(mint a) { return invprd(a.a); }
mint prd(mint a) { return prd(a.a); }
mint modpow(mint a, ll n) { return modpow(a.a, n); }
mint C(int a, int b) {
    if(a < 0 || b < 0) return 0;
    if(a < b) return 0;
    return prd(a) * invprd(b) * invprd(a - b);
}
mint P(int a, int b) {
    if(a < 0 || b < 0) return 0;
    if(a < b) return 0;
    return prd(a) * invprd(a - b);
}
ostream &operator<<(ostream &os, mint a) {
    os << a.a;
    return os;
}
istream &operator>>(istream &is, mint &a) {
    ll x;
    is >> x;
    a = x;
    return is;
}
struct modinfo {
    int mod, root;
};
constexpr modinfo base0{1045430273, 3};
constexpr modinfo base1{1051721729, 6};
constexpr modinfo base2{1053818881, 7};
using mint0 = modint<base0.mod>;
using mint1 = modint<base1.mod>;
using mint2 = modint<base2.mod>;
using Poly = vmint;
template <int mod> void FMT(vector<modint<mod>> &f, bool inv = false) {
    using V = vector<modint<mod>>;
    static V g(30), ig(30);
    if(g.front().a == 0) {
        modint<mod> root = 2;
        while((root.pow((mod - 1) / 2)).a == 1) root += 1;
        rep(i, 30) g[i] = -(root.pow((mod - 1) >> (i + 2))), ig[i] = g[i].inv();
    }
    int n = size(f);
    if(!inv) {
        for(int m = n; m >>= 1;) {
            modint<mod> w = 1;
            for(int s = 0, k = 0; s < n; s += 2 * m) {
                for(int i = s, j = s + m; i < s + m; ++i, ++j) {
                    auto x = f[i], y = f[j] * w;
                    if(x.a >= mod) x.a -= mod;
                    f[i].a = x.a + y.a, f[j].a = x.a + (mod - y.a);
                }
                w *= g[__builtin_ctz(++k)];
            }
        }
    } else {
        for(int m = 1; m < n; m *= 2) {
            modint<mod> w = 1;
            for(int s = 0, k = 0; s < n; s += 2 * m) {
                for(int i = s, j = s + m; i < s + m; ++i, ++j) {
                    auto x = f[i], y = f[j];
                    f[i] = x + y, f[j].a = x.a + (mod - y.a), f[j] *= w;
                }
                w *= ig[__builtin_ctz(++k)];
            }
        }
    }
    modint<mod> c;
    if(inv)
        c = modint<mod>(n).inv();
    else
        c = 1;
    for(auto &&e : f) e *= c;
}
Poly operator-(Poly f) {
    for(auto &&e : f) e = -e;
    return f;
}
Poly &operator+=(Poly &l, const Poly &r) {
    l.resize(max(l.size(), r.size()));
    rep(i, r.size()) l[i] += r[i];
    return l;
}
Poly operator+(Poly l, const Poly &r) { return l += r; }
Poly &operator-=(Poly &l, const Poly &r) {
    l.resize(max(l.size(), r.size()));
    rep(i, r.size()) l[i] -= r[i];
    return l;
}
Poly operator-(Poly l, const Poly &r) { return l -= r; }
Poly &operator<<=(Poly &f, size_t n) { return f.insert(f.begin(), n, 0), f; }
Poly operator<<(Poly f, size_t n) { return f <<= n; }
Poly &operator>>=(Poly &f, size_t n) { return f.erase(f.begin(), f.begin() + min(f.size(), n)), f; }
Poly operator>>(Poly f, size_t n) { return f >>= n; }

constexpr int mod0 = 998244353, mod1 = 1300234241, mod2 = 1484783617;
using M0 = modint<mod0>;
using M1 = modint<mod1>;
using M2 = modint<mod2>;

template <int mod> void mul(vector<modint<mod>> &l, vector<modint<mod>> &r) {
    int n = size(l), m = size(r), sz = 1 << __lg(2 * (n + m - 1) - 1);
    l.resize(sz), FMT<mod>(l);
    r.resize(sz), FMT<mod>(r);
    rep(i, sz) l[i] *= r[i];
    FMT<mod>(l, true);
    l.resize(n + m - 1);
}
Poly operator*(const Poly &l, const Poly &r) {
    if(l.empty() or r.empty()) return Poly();
    int n = size(l), m = size(r), sz = 1 << __lg(2 * (n + m - 1) - 1);
    vector<M0> l0(n), r0(m);
    vector<M1> l1(n), r1(m);
    vector<M2> l2(n), r2(m);
    rep(i, n) l0[i] = l[i].a, l1[i] = l[i].a, l2[i] = l[i].a;
    rep(i, m) r0[i] = r[i].a, r1[i] = r[i].a, r2[i] = r[i].a;
    mul<mod0>(l0, r0), mul<mod1>(l1, r1), mul<mod2>(l2, r2);
    Poly res(n + m - 1);
    // garner
    static constexpr M1 inv0 = 613999507;
    static constexpr M2 inv1 = 1147332803, inv0m1 = 45381342;
    static constexpr mint m0 = mod0, m0m1 = m0 * mod1;
    rep(i, n + m - 1) {
        int y0 = l0[i].a;
        int y1 = (inv0 * (l1[i] - y0)).a;
        int y2 = (inv0m1 * (l2[i] - y0) - inv1 * y1).a;
        res[i] = m0 * y1 + m0m1 * y2 + y0;
    }
    return res;
}
Poly &operator*=(Poly &l, const Poly &r) { return l = l * r; }
Poly integ(const Poly &f) {
    Poly res(f.size() + 1);
    for(int i = 1; i < (int)res.size(); ++i) res[i] = f[i - 1] / i;
    return res;
}
// Poly deriv(const Poly &f) {
//     if(f.size() == 0) return Poly();
//     Poly res(f.size() - 1);
//     rep(i, res.size()) res[i] = f[i + 1] * (i + 1);
//     return res;
// }
ostream &operator<<(ostream &os, Poly a) {
    for(auto e : a) cout << e.a << " ";
    return os;
}
} // namespace modular
using namespace modular;

int main() {
    TEST {
        INT(_a, l, r);
        if(_a == 1) {
            cout << r - l + 1 << endl;
            continue;
        }
        mint a = _a;
        Matrix<mint> A(2);
        A[0][0] = A[1][1] = a;
        A[0][1] = (mint)a * a - 1;
        A[1][0] = 1;
        auto I = A.I(2);
        Matrix<mint> inv = I - A;
        swap(inv[0][0], inv[1][1]);
        inv[0][1] *= -1, inv[1][0] *= -1;
        Matrix<mint> x(2, 1);
        x[0][0] = 1;
        auto E = (I - A) * inv;
        auto f = [&](int k) -> mint {
            if(k == -1) return 0;
            return (((I - (A ^ (k + 1))) * inv) * x)[0][0] * modpow(1 - a.a, MOD - 2);
        };
        cout << f(r) - f(l - 1) - (r - l + 1) << endl;
    }
}
accuracy: 1
time_ms: 80
memory_kb: 3552
final_rank: 12

code_snippet: #include<bits/stdc++.h>

int main(){
    using namespace std;
    using f_1e9_7_va = pair<unsigned long, unsigned long>;
    constexpr unsigned long MOD = 1000000007;
    const unsigned long Q{[]{unsigned long ret; cin >> ret; return ret;}()};
    const auto& modinv = [](unsigned long a, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD}, n{MOD - 2};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };
    for(unsigned long i{0}, a, l, r; i < Q; ++i)cout << [&modinv, &a, &l, &r]{
        cin >> a >> l >> r;
        ++r;
        if(a == 1)return r - l;
        const auto& mul = [&a](const f_1e9_7_va& x, const f_1e9_7_va& y) -> f_1e9_7_va {
            f_1e9_7_va ret{x.first * y.first + MOD - x.second * y.second % MOD, x.first * y.second + x.second * y.first + 2 * a * x.second % MOD * y.second};
            ret.first %= MOD;
            ret.second %= MOD;
            return ret;
        };
        const auto& modpow = [&mul](const f_1e9_7_va& a, unsigned long n, const f_1e9_7_va& b = {1, 0}) -> f_1e9_7_va {
            f_1e9_7_va ret{b}, al{a};
            while(n){
                if(n & 1)ret = mul(ret, al);
                al = mul(al, al);
                n >>= 1;
            }
            return ret;
        };
        const auto& subtract = [](const f_1e9_7_va& a, const f_1e9_7_va& b) -> f_1e9_7_va {
            return {(a.first + MOD - b.first) % MOD, (a.second + MOD - b.second) % MOD};
        };
        unsigned long k{modinv(2 * a - 2)}, m{(2 * MOD - 1 - k) % MOD};
        f_1e9_7_va tmp{m, k}, x{modpow({0, 1}, r, tmp)}, y{modpow({0, 1}, l, tmp)}, z{subtract(x, y)};
        return (2 * (z.first + a * z.second) + l + MOD - r) % MOD;
    }() << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3080
final_rank: 1

code_snippet: #include<bits/stdc++.h>

int main(){
    using namespace std;
    using f_1e9_7_va = pair<unsigned long, unsigned long>;
    constexpr unsigned long MOD = 1000000007;
    const unsigned long Q{[]{unsigned long ret; cin >> ret; return ret;}()};
    const auto& modinv = [](unsigned long a, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD}, n{MOD - 2};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };
    for(unsigned long i{0}, a, l, r; i < Q; ++i)cout << [&modinv, &a, &l, &r]{
        cin >> a >> l >> r;
        ++r;
        if(a == 1)return r - l;
        const auto& mul = [&a](const f_1e9_7_va& x, const f_1e9_7_va& y) -> f_1e9_7_va {
            f_1e9_7_va ret{x.first * y.first + MOD - x.second * y.second % MOD, x.first * y.second + x.second * y.first + 2 * a * x.second % MOD * y.second};
            ret.first %= MOD;
            ret.second %= MOD;
            return ret;
        };
        const auto& modpow = [&mul](const f_1e9_7_va& a, unsigned long n, const f_1e9_7_va& b = {1, 0}) -> f_1e9_7_va {
            f_1e9_7_va ret{b}, al{a};
            while(n){
                if(n & 1)ret = mul(ret, al);
                al = mul(al, al);
                n >>= 1;
            }
            return ret;
        };
        const auto& subtract = [](const f_1e9_7_va& a, const f_1e9_7_va& b) -> f_1e9_7_va {
            return {(a.first + MOD - b.first) % MOD, (a.second + MOD - b.second) % MOD};
        };
        unsigned long k{modinv(2 * a - 2, 1)}, m{(2 * MOD - 1 - k) % MOD};
        f_1e9_7_va tmp{m, k}, x{modpow({0, 1}, r, tmp)}, y{modpow({0, 1}, l, tmp)}, z{subtract(x, y)};
        return (2 * (z.first + a * z.second) + l + MOD - r) % MOD;
    }() << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3416
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

mat matmul(ll a,ll b,ll c,mat &A,mat &B){
  mat C(a,vec(c,0));
  rep(i,a)rep(j,c){
    rep(k,b){
      C.at(i).at(j)+=A.at(i).at(k)*B.at(k).at(j);
    }
    C.at(i).at(j)%=MOD;
  }
  return C;
}
mat matpow(mat A,ll n,ll k){
  if (n == 0){
    mat B(k,vec(k,0));
    rep(i,k){
      B.at(i).at(i)=1;
    }
    return B;
  }
  if (n == 1) return A;
  if (n % 2 == 1){
    mat t=matpow(A,n-1,k);
    return matmul(k,k,k,A,t);
  }
  mat t=matpow(A,n/2,k);
  return matmul(k,k,k,t,t);
}


int main(){
  ll Q;
  cin >> Q;
  rep(i,Q){
    ll a,r,l;
    cin >> a >> l >> r;
    if(a==1){
      cout << r-l+1 << endl;
      continue;
    }
    mat G(3,vec(3)),A(3,vec(1));
    A.at(0).at(0)=2*a+2;
    A.at(1).at(0)=2*a;
    A.at(2).at(0)=2;
    G.at(0).at(0)=1;
    G.at(0).at(1)=2*a;
    G.at(0).at(2)=-1;
    G.at(1).at(0)=0;
    G.at(1).at(1)=2*a;
    G.at(1).at(2)=-1;
    G.at(2).at(0)=0;
    G.at(2).at(1)=1;
    G.at(2).at(2)=0;
    ll R,L;
    if(r>=1){
      mat X=matpow(G,r-1,3);
      mat Y=matmul(3,3,1,X,A);
      R=Y.at(0).at(0);
    }else{
      R=2;
    }
    if(l>1){
      mat X=matpow(G,l-2,3);
      mat Y=matmul(3,3,1,X,A);
      L=Y.at(0).at(0);
    }else if(l==1){
      L=2;
    }else{
      L=0;
    }
    cout << (R-L-(r-l+1)+5*MOD)%MOD << endl;
  }
    
    
    
}
accuracy: 1
time_ms: 190
memory_kb: 3428
final_rank: 13