Language: C++

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

const int nmax=1<<17;
const ll mod=1000000007;

//////////
ll gcd(ll a,ll b){
	return (b==0)?a:gcd(b,a%b);
}

ll lcm(ll a,ll b){
	ll g=gcd(a,b);
	return a/g*b;
}

ll inv(ll a){
	ll n=mod-2;
	ll b=1LL;
	while(n){
		if(n&1) b=b*a%mod;
		a=a*a%mod;
		n>>=1;
	}
	return b;
}

/////////

int p[nmax];
ll tsum[nmax];
ll tcycle[nmax];

int size;
ll sum[2*nmax-1];
ll cycle[2*nmax-1];


void init(int n){
	size=1;
	while(size< n) size*=2;
	for(int i=size;i<2*size-1; i++) cycle[i]=1LL,sum[i]=0LL;
	for(int i=size-1;i<n+size-1;++i){
		sum[i]=(tsum[i-(size-1)]*inv(tcycle[i-(size-1)]))%mod;
		cycle[i]=tcycle[i-(size-1)];
	}
	for(int i=size-2;i>=0;--i){
		sum[i]=(sum[2*i+1]+sum[2*i+2])%mod;
		cycle[i]=lcm(cycle[2*i+1],cycle[2*i+2]);
	}
}

ll lcm_query(int a, int b, int k,int l,int r){
	if(r<=a||b<=l) return 1LL;

	if(a<=l&&r<=b)
		return cycle[k];
	else{
		ll vl =lcm_query(a,b,k*2+1,l,(l+r)/2);
		ll vr =lcm_query(a,b,k*2+2,(l+r)/2,r);
		return lcm(vl,vr);
	}
}

ll sum_query(int a, int b, int k,int l,int r){
	if(r<=a||b<=l) return 0LL;

	if(a<=l&&r<=b)
		return sum[k];
 	else{
		ll vl =sum_query(a,b,k*2+1,l,(l+r)/2);
		ll vr =sum_query(a,b,k*2+2,(l+r)/2,r);
		return (vl+vr)%mod;
	}
}


int main(void){
	int n,q;
	cin >> n >> q;
	n++;
	for(int i=1;i<n;++i) cin >> p[i];

	for(int i=1;i<n;++i){
		vi order;
		int cur=p[i],csum=p[i],cyc=1;
		order.pb(cur);
		while(cur!=i){
			cur=p[cur];
			csum+=cur;
			cyc++;
			order.pb(cur);
		}
		for(auto &j:order) tcycle[j]=cyc,tsum[j]=csum;
	}
	cycle[0]=1LL;

	init(n);

	rep(loop,q){
		int l,r;
		cin >> l >> r;
		r++;
		ll num=lcm_query(l,r,0,0,size)%mod;
		ll den=sum_query(l,r,0,0,size)%mod;
		cout << num*den%mod << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:68:9: error: reference to 'size' is ambiguous
   68 |         size=1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:69:15: error: reference to 'size' is ambiguous
   69 |         while(size< n) size*=2;
      |               ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:69:24: error: reference to 'size' is ambiguous
   69 |         while(size< n) size*=2;
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:70:19: error: reference to 'size' is ambiguous
   70 |         for(int i=size;i<2*size-1; i++) cycle[i]=1LL,sum[i]=0LL;
      |                   ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:70:28: error: reference to 'size' is ambiguous
   70 |         for(int i=size;i<2*size-1; i++) cycle[i]=1LL,sum[i]=0LL;
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:71:19: error: reference to 'size' is ambiguous
   71 |         for(int i=size-1;i<n+size-1;++i){
      |                   ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:71:30: error: reference to 'size' is ambiguous
   71 |         for(int i=size-1;i<n+size-1;++i){
      |                              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:72:33: error: reference to 'size' is ambiguous
   72 |                 sum[i]=(tsum[i-(size-1)]*inv(tcycle[i-(size-1)]))%mod;
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:72:56: error: reference to 'size' is ambiguous
   72 |                 sum[i]=(tsum[i-(size-1)]*inv(tcycle[i-(size-1)]))%mod;
      |                                                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:73:36: error: reference to 'size' is ambiguous
   73 |                 cycle[i]=tcycle[i-(size-1)];
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:75:19: error: reference to 'size' is ambiguous
   75 |         for(int i=size-2;i>=0;--i){
      |                   ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc: In function 'int main()':
a.cc:132:42: error: reference to 'size' is ambiguous
  132 |                 ll num=lcm_query(l,r,0,0,size)%mod;
      |                                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~
a.cc:133:42: error: reference to 'size' is ambiguous
  133 |                 ll den=sum_query(l,r,0,0,size)%mod;
      |                                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:62:5: note:                 'int size'
   62 | int size;
      |     ^~~~