Language: C++

#include <vector>
#include <iostream>

#define mod 1000000007

using namespace std;

long long gcd(long long, long long);
long long lcm(long long, long long);

int extgcd(int, int, int, int);

int mod_inv(int a);

class SegTree
{
private:

	int size;

	vector<long long> lcmvalue;

	long long _query(int a, int b, int k, int l, int r)
	{
		if (r <= a || b <= l) { return 1; }
		if (a <= l && r <= b) { return lcmvalue[k]; }

		long long vl = _query(a, b, 2 * k, l, (l + r) / 2);
		long long vr = _query(a, b, 2 * k + 1, (l + r) / 2, r);

		return lcm(vl, vr) % mod;
	}

public:

	SegTree(int size_)
	{
		for (size = 1; size < size_;) { size <<= 1; }

		lcmvalue = vector<long long>(size * 2, 1);
	}

	void update(int i, long long x)
	{
		i += size;

		lcmvalue[i] = x;

		while (i > 1)
		{
			i = i / 2;

			lcmvalue[i] = lcm(lcmvalue[2 * i], lcmvalue[2 * i + 1]) % mod;
		}
	}

	long long query(int s, int t)
	{
		return _query(s, t, 1, 0, size);
	}
};

long long gcd(long long a, long long b)
{
	if (a == 0) { return b; }
	if (b == 0) { return a; }

	if (a > b) { return gcd(b, a % b); }

	return gcd(a, b % a);
}

long long lcm(long long a, long long b)
{
	return (a * b % mod * mod_inv(gcd(a, b))) % mod;
}

int extgcd(int a, int b, int &x, int &y)
{
	int d = a;

	if (b != 0)
	{
		d = extgcd(b, a % b, y, x);

		y -= (a / b) * x;
	}
	else
	{
		x = 1; y = 0;
	}

	return d;
}

int mod_inv(int a)
{
	int x, y;

	extgcd(a, mod, x, y);

	return (mod + x % mod) % mod;
}

vector<int> G; // i --> G[i] Graph

vector<bool> visited;

vector<vector<int> > cycles;

void rec(int p)
{
	visited[p] = true;

	cycles.back().push_back(p);

	if (!visited[G[p]])
	{
		rec(G[p]);
	}
}

int main()
{
	int V, Q;

	scanf("%d", &V);
	scanf("%d", &Q);

	G = vector<int>(V);

	for (int i = 0; i < V; i++)
	{
		scanf("%d", &G[i]); G[i]--; // 1 -> 0 indexed
	}

	visited = vector<bool>(V, false);

	for (int i = 0; i < V; i++)
	{
		if (!visited[i])
		{
			cycles.push_back(vector<int>());

			rec(i);
		}
	}

	vector<long long> val(V, 0), num(V, 0);

	for (int i = 0; i < cycles.size(); i++)
	{
		long long sum = 0;

		for (int j = 0; j < cycles[i].size(); j++)
		{
			sum = (sum + cycles[i][j] + 1) % mod;
		}

		for (int j = 0; j < cycles[i].size(); j++)
		{
			val[cycles[i][j]] = sum;

			num[cycles[i][j]] = cycles[i].size();
		}
	}

	SegTree s(V);

	for (int i = 0; i < V; i++)
	{
		s.update(i, num[i]);
	}

	int l, r; vector<int> inv(V + 1, 0);

	for (int i = 1; i <= V; i++)
	{
		inv[i] = mod_inv(i);
	}

	for (int i = 0; i < Q; i++)
	{
		scanf("%d", &l); l--; // 1 -> 0 indexed
		scanf("%d", &r); r--;

		long long rep = s.query(l, r + 1);

		long long ret = 0;

		for (int j = l; j <= r; j++)
		{
			ret = (ret + (val[j] * rep) % mod * inv[num[j]] % mod) % mod;
		}

		printf("%lld\n", ret);
	}

	return 0;
}

Compiler output:
a.cc: In function 'int extgcd(int, int, int&, int&)':
a.cc:84:27: error: call of overloaded 'extgcd(int&, int, int&, int&)' is ambiguous
   84 |                 d = extgcd(b, a % b, y, x);
      |                     ~~~~~~^~~~~~~~~~~~~~~~
a.cc:11:5: note: candidate: 'int extgcd(int, int, int, int)'
   11 | int extgcd(int, int, int, int);
      |     ^~~~~~
a.cc:78:5: note: candidate: 'int extgcd(int, int, int&, int&)'
   78 | int extgcd(int a, int b, int &x, int &y)
      |     ^~~~~~
a.cc: In function 'int mod_inv(int)':
a.cc:100:15: error: call of overloaded 'extgcd(int&, int, int&, int&)' is ambiguous
  100 |         extgcd(a, mod, x, y);
      |         ~~~~~~^~~~~~~~~~~~~~
a.cc:11:5: note: candidate: 'int extgcd(int, int, int, int)'
   11 | int extgcd(int, int, int, int);
      |     ^~~~~~
a.cc:78:5: note: candidate: 'int extgcd(int, int, int&, int&)'
   78 | int extgcd(int a, int b, int &x, int &y)
      |     ^~~~~~