#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
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;}
#define MOD 1000000007ll
struct ModInt{
	ll val;
	ModInt():val(0ll){}
	ModInt(int v):val(((v%MOD)+MOD)%MOD){}
	ModInt(ll v):val(((v%MOD)+MOD)%MOD){}
	ModInt exp(ll y)const{
		if(!y)return ModInt(1ll);
		ModInt a = exp(y/2ll);
		a *= a;
		if(y&1)a*=(*this);
		return a;
	}
	bool operator==(const ModInt& x)const{return val==x.val;}
	inline bool operator!=(const ModInt& x)const{return !(*this==x);}
	bool operator<(const ModInt& x)const{return val<x.val;}
	bool operator>(const ModInt& x)const{return val>x.val;}
	inline bool operator>=(const ModInt& x)const{return !(*this<x);}
	inline bool operator<=(const ModInt& x)const{return !(*this>x);}
	ModInt& operator+=(const ModInt& x){if((val+=x.val)>=MOD)val-=MOD;return *this;}
	ModInt& operator-=(const ModInt& x){if((val+=MOD-x.val)>=MOD)val-=MOD;return *this;}
	ModInt& operator*=(const ModInt& x){(val*=x.val)%=MOD;return *this;}
	ModInt operator+(const ModInt& x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt& x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt& x)const{return ModInt(*this)*=x;}
};
istream& operator>>(istream&i,ModInt&x){i>>x.val;return i;}
ostream& operator<<(ostream&o,const ModInt&x){o<<x.val;return o;}
ll gcd(ll a,ll b){
	if(b==0ll)return a;
	else return gcd(b,a%b);
}
ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}
int N,Q;
vector<ll> p;
int used[100100];
int cnt[100100];
int c[100100];
ll sum[100100];
const int SIZE = 1<<17;
struct segtree{
	ll seg[SIZE*2];
	void init(){
		for(int i=0;i<SIZE*2;i++)seg[i]=1ll;
		for(int i=0;i<N;i++){
			seg[i+SIZE-1]=(ll)c[i];
		}
		for(int i=SIZE-2;i>=0;i--){
			seg[i]=lcm(seg[i*2+1],seg[i*2+2]);
		}
	}
	ll query(int a,int b,int k=0,int l=0,int r=SIZE){
		if(r<=a||b<=l)return 1;
		else if(a<=l&&r<=b){
			return seg[k];
		}else{
			ll lch = query(a,b,k*2+1,l,(l+r)/2);
			ll rch = query(a,b,k*2+2,(l+r)/2,r);
			return lcm(lch,rch);
		}
	}
}seg;
#define MAX_N 400100
struct BIT{
	ll bit[MAX_N+1];
	void add(int i,ll x){
		i++;
		while(i<=MAX_N){
			bit[i]+=x;
			i+=i&-i;
		}
		return;
	}
	ll sum(int i){
		i++;
		ll res=0;
		while(i>0){
			res+=bit[i];
			i-=i&-i;
		}
		return res;
	}
	ll sum(int l,int r){ //[l,r]
		if(l==0)return sum(r);
		else return sum(r)-sum(l-1);
	}
}bit[45];
int main(){
	scanf("%d %d",&N,&Q);
	p.resize(N);
	for(int i=0;i<N;i++)scanf("%lld",&p[i]);
	for(int i=0;i<N;i++)p[i]--;
	memset(used,-1,sizeof(used));
	int id = 0;
	for(int i=0;i<N;i++){
		if(used[i]!=-1)continue;
		int pos = i;
		while(used[pos]==-1){
			used[pos]=id;
			cnt[id]++;
			sum[id]+=pos;
			pos = (int)p[pos];
		}
		id++;
	}
	for(int i=0;i<N;i++)c[i]=cnt[used[i]];
	seg.init();
	for(int i=0;i<N;i++){
		bit[c[i]].add(i,sum[used[i]]);
	}
	for(int i=0;i<Q;i++){
		int l,r;
		scanf("%d %d",&l,&r);
		l--;r--;
		ll K = seg.query(l,r+1);
		ModInt ans(K);
		ans *= ModInt(r-l+1);
		for(int j=1;j<=40;j++){
			ans += ModInt(bit[j].sum(l,r))*ModInt(K/(ll)j);
		}
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 38724