#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

void prime_factor(ll N,vi& dev,vi& id)
{
	for(ll i=2;i*i<=N;i++){
		int cnt = 0;
		while(N%i == 0){
			cnt++;
			N /= i;
		}
		if(cnt){
			dev.pb(i);
			id.pb(cnt);
		}
	}
	if(N != 1){
		dev.pb(N);
		id.pb(1);
	}
	return;
}

ll mod_pow(ll a,ll b)
{
    a %= MOD;
    ll res = 1;
    while(b){
        if(b & 1){
            res = res * a % MOD;
        }
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

ll add(ll x,ll y)
{
    return (x + y)%MOD;
}

ll sub(ll x,ll y)
{
    return (x+MOD-y)%MOD;
}

ll mul(ll x,ll y)
{
    return x*y%MOD;
}

template<typename V> class segtree {
private:
    int n,sz;
    vector<V> node;
	vector<ll> val;
	vi zip;
	int unzip[40];
public:
    segtree(vector<V>& v,vector<ll>& arg){
		zip = {2,3,5,7,11,13,17,19,23,29,31,37};
		rep(i,12){
			unzip[zip[i]] = i;
		}
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1,vi(12,0));
		val.resize(2*n-1,0);
        rep(i,sz){
            node[i+n-1] = v[i];
			val[i+n-1] = arg[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i].resize(12,0);
			ll tm1 = val[i*2+1],tm2 = val[i*2+2];
            rep(j,12){
				ll hoge = max(node[i*2+1][j],node[i*2+2][j]);
                node[i][j] = hoge;
				tm1 = mul(tm1,mod_pow(zip[j],hoge-node[i*2+1][j]));
				tm2 = mul(tm2,mod_pow(zip[j],hoge-node[i*2+2][j]));
            }
			val[i] = add(tm1,tm2);
        }
    }
    pair<V,ll> query(int a,int b,int k=0,int l=0,int r=-1)
    {
        if(r < 0) r = n;
    	if(r <= a || b <= l){
			V hoge(12,0);
    		return make_pair(hoge,0);
    	}
    	if(a <= l && r <= b){
    		return make_pair(node[k],val[k]);
    	}else{
    		pair<V,ll> vl = query(a,b,2*k+1,l,(l+r)/2);
    		pair<V,ll> vr = query(a,b,2*k+2,(l+r)/2,r);
			V& u = vl.fi,v = vr.fi;
			V res(12,0);
			ll tm1 = vl.se,tm2 = vr.se;
    		rep(i,12){
				ll hoge = max(u[i],v[i]);
				tm1 = mul(tm1,mod_pow(zip[i],hoge-u[i]));
				tm2 = mul(tm2,mod_pow(zip[i],hoge-v[i]));
				res[i] = hoge;
			}
			return make_pair(res,add(tm1,tm2));
    	}
    }
    // void print()
    // {
    //     rep(i,sz){
    //         cout << query(i,i+1) << " ";
    //     }
    //     cout << endl;
    // }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,q;
    cin >> n >> q;
    vi p(n);
    rep(i,n){
        cin >> p[i];
		--p[i];
    }
	int zip[] = {2,3,5,7,11,13,17,19,23,29,31,37};
	int unzip[40];
	rep(i,12){
		unzip[zip[i]] = i;
	}
	vvi vec(n,vi(12,0));
	vl hoge(n,0);
    rep(i,n){
        int nw = p[i];
        int res = 1;
        while(nw != i){
			hoge[i] += nw+1;
            nw = p[nw];
            res++;
        }
		hoge[i] += nw+1;
		vi r1,r2;
		prime_factor(res,r1,r2);
		rep(j,len(r1)){
			vec[i][unzip[r1[j]]] = r2[j];
		}
    }
	segtree<vi> st(vec,hoge);
	rep(i,q){
		int l,r;
		cin >> l >> r;
		--l,--r;
		cout << st.query(l,r+1).se << "\n";
	}
    return 0;
}

Measured memory_bytes: 37196