#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long int;
const ll MOD = 1000000007;

// 抽象 SegmentTree (0-indexed・一点更新・区間取得)
template <typename MonoidType>
struct SegmentTree {
    using Function = function< MonoidType(MonoidType, MonoidType) >;

    // node, identity element
    int n;
    vector<MonoidType> node;
    MonoidType E0;

    // update / combine function
    Function upd_f, cmb_f;

    void build(int m, vector<MonoidType> v = vector<MonoidType>()) {
        if(v != vector<MonoidType>()) m = v.size();
        n = 1; while(n < m) n *= 2;

        node = vector<MonoidType>(2*n-1, E0);
        if(v != vector<MonoidType>()) {
            for(int i=0; i<m; i++) {
                node[n-1+i] = v[i];
            }
            for(int i=n-2; i>=0; i--) {
                node[i] = cmb_f(node[2*i+1], node[2*i+2]);
            }
        }
    }

    // initialize
    SegmentTree() {}
    SegmentTree(int n_, MonoidType E0_,
                Function upd_f_, Function cmb_f_,
                vector<MonoidType> v = vector<MonoidType>()) :
        E0(E0_), upd_f(upd_f_), cmb_f(cmb_f_) {
        build(n_, v);
    }

    // update k-th element (applied value: x)
    void update(int k, MonoidType x) {
        k += n - 1;
        node[k] = upd_f(node[k], x);
        while(k > 0) {
            k = (k - 1) / 2;
            node[k] = cmb_f(node[2*k+1], node[2*k+2]);
        }
    }

    // range query for [a, b)
    // 非再帰のアイデア: http://d.hatena.ne.jp/komiyam/20131202/1385992406
    MonoidType query(int a, int b) {
        MonoidType vl = E0, vr = E0;
        for(int l=a+n, r=b+n; l<r; l>>=1, r>>=1) {
            if(l & 1) vl = cmb_f(vl, node[(l++)-1]);
            if(r & 1) vr = cmb_f(node[(--r)-1], vr);
        }
        return cmb_f(vl, vr);
    }
};

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

int main() {
    int N, Q; cin >> N >> Q;
    vector<int> P(N);
    for(int i=0; i<N; i++) cin >> P[i], P[i]--;
    
    // [L, R] に関与する要素すべてについて
    // ・その要素が属するサイクルの総和 (るいせきわ)
    // ・その要素が属するサイクルのサイズの lcm (long long のセグ木で OK)
    // を得たい

    vector<ll> cyc(N), cyc_sum(N);
    vector<bool> visited(N);
    for(int i=0; i<N; i++) {
        if(visited[i]) continue;
        vector<int> pass;

        ll v = i, s = 0;
        while(!visited[v]) {
            visited[v] = true; pass.emplace_back(v);
            s += P[v] + 1;
            v = P[v];
        }

        for(auto e : pass) {
            cyc[e] = pass.size();
            cyc_sum[e] = s;
        }
    }

    SegmentTree<ll> seg(N, 1,
                        [](ll a, ll b) { return a; },
                        [](ll a, ll b) { return a / gcd(a, b) * b; },
                        cyc);

    const int K = 40;
    vector< vector<ll> > sum(K+1, vector<ll>(N+1));
    for(int i=0; i<N; i++) {
        for(int k=1; k<=K; k++) sum[k][i+1] += sum[k][i];
        sum[ cyc[i] ][i+1] += cyc_sum[i];
    }

    while(Q--) {
        int l, r; cin >> l >> r; l--;
        ll lcm = seg.query(l, r);
        ll res = 0;
        for(int k=1; k<=K; k++) {
            ll s = (sum[k][r] - sum[k][l]) % MOD;
            (res += lcm / k % MOD * s % MOD) %= MOD;
        }
        cout << res << endl;
    }
    return 0;
}



Measured memory_bytes: 39764