Language: C++

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef __int128 ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;
const ll MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll gcd(ll a, ll b)
{
	if (a < b) swap(a, b);
	return (a%b ? gcd(a%b, b) : b);
}

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

struct Data
{
	pair<ll, ll> num;
	Data() :num(pii(1, 0)) {};
	Data(ll n, ll m) :num(pii(n, m)) {};
};

ll tlcm;

struct SegmentTree
{
	int n; vector<Data> data;
	SegmentTree(int N)
	{
		n = 1;
		while (n < N) n *= 2;
		data.resize(2 * n);
	}
private:
	ll sub(int left, int right, int node, int nleft, int nright)
	{
		if (nright <= left || right <= nleft) return 0;
		if (left <= nleft && nright <= right) return (tlcm / data[node].num.first*data[node].num.second) % MOD;
		ll vl = sub(left, right, node * 2 + 1, nleft, (nleft + nright) / 2);
		ll vr = sub(left, right, node * 2 + 2, (nleft + nright) / 2, nright);
		vl %= MOD; vr %= MOD;
		return (vl + vr) % MOD;;
	}

	ll seglcm(int left, int right, int node, int nleft, int nright)
	{
		if (nright <= left || right <= nleft) return 1;
		if (left <= nleft && nright <= right) return data[node].num.first;
		ll vl = seglcm(left, right, node * 2 + 1, nleft, (nleft + nright) / 2);
		ll vr = seglcm(left, right, node * 2 + 2, (nleft + nright) / 2, nright);
		return lcm(vl, vr);
	}

public:
	void update(int pos, Data value)
	{
		pos += n - 1;
		data[pos] = value;
		while (pos > 0)
		{
			pos = (pos - 1) / 2;
			data[pos].num.first = lcm(data[pos * 2 + 1].num.first, data[pos * 2 + 2].num.first);
			ll l = data[pos * 2 + 1].num.second * data[pos].num.first / data[pos * 2 + 1].num.first;
			ll r = data[pos * 2 + 2].num.second * data[pos].num.first / data[pos * 2 + 2].num.first;
			l %= MOD, r %= MOD;
			data[pos].num.second = (l + r) % MOD;
		}
	}
	ll sum_query(int left, int right)
	{
		tlcm = seglcm(left, right, 0, 0, n);
		return sub(left, right, 0, 0, n);
	}
};

struct UnionFind
{
	vector<pii> par;
	UnionFind(int n) 
	{
		REP(i, n) par.emplace_back(-i, 1); 
	}
	int find(int x) { return par[x].first < 0 ? x : par[x].first = find(par[x].first); }
	void unite(int x, int y)
	{
		x = find(x); y = find(y);
		if (x == y) return;
		par[x].first += par[y].first;
		par[x].second += par[y].second;
		par[y].first = x;
		if (find(x) != find(y)) par[find(y)].first = x;
	}
	bool same(int x, int y)
	{
		return find(x) == find(y);
	}
};


int main()
{
	int n, q;
	cin >> n >> q;
	vi v(n);
	REP(i, n) cin >> v[i];
	UnionFind uf(n+1);
	REP(i, n) uf.unite(i + 1, v[i]);
	SegmentTree seg(n);
	REP(i, n)
	{
		int root = uf.find(i + 1);
		long long sum = -uf.par[root].first, cm = uf.par[root].second;
		seg.update(i, Data(cm, sum%MOD));
	}
	REP(i, q)
	{
		int a, b; cin >> a >> b;
		cout << seg.sum_query(a - 1, b) % MOD << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:134:22: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'll' {aka '__int128'})
  134 |                 cout << seg.sum_query(a - 1, b) % MOD << endl;
      |                 ~~~~ ^~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                 |                               |
      |                 |                               ll {aka __int128}
      |                 std::ostream {aka std::basic_ostream<char>}
In file included from /usr/include/c++/14/istream:41,
                 from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127,
                 from a.cc:1:
/usr/include/c++/14/ostream:174:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  174 |       operator<<(long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:178:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  178 |       operator<<(unsigned long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:182:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  182 |       operator<<(bool __n)
      |       ^~~~~~~~
In file included from /usr/include/c++/14/ostream:1022:
/usr/include/c++/14/bits/ostream.tcc:96:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]'
   96 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:189:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  189 |       operator<<(unsigned short __n)
      |       ^~~~~~~~
/usr/include/c++/14/bits/ostream.tcc:110:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]'
  110 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:200:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  200 |       operator<<(unsigned int __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:211:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  211 |       operator<<(long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:215:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  215 |       operator<<(unsigned long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:231:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  231 |       operator<<(double __f)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:235:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  235 |       operator<<(float __f)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:243:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  243 |       operator<<(long double __f)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:573:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, char) [with _CharT = char; _Traits = char_traits<char>]'
  573 |     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:579:5: note: candidate: 'std::basic_ostream<char, _Traits>& std::operator<<(basic_ostream<char, _Traits>&, char) [with _Traits = char_traits<char>]'
  579 |     operator<<(basic_ostream<char, _Traits>& __out, char __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:590:5: note: candidate: 'std::basic_ostream<char, _Traits>& std::operator<<(basic_ostream<char, _Traits>&, signed char) [with _Traits = char_traits<char>]'
  590 |     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:595:5: note: candidate: 'std::basic_ostream<char, _Traits>& std::operator<<(basic_ostream<char, _Traits>&, unsigned char) [with _Traits = char_traits<char>]'
  595 |     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
      |     ^~~~~~~~