extern crate core;

use std::fmt;
use std::i32::{MAX};
use std::cmp::{Ordering, min, max };
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign, Range};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet, HashMap};
use std::fmt::{Display, Formatter, Error};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_lines{
    ($count: expr; $delimiter: expr; $ty: ty) => {{
        let line_count = $count;
        let mut vec: Vec<Vec<$ty>> = Vec::with_capacity(line_count);
        for _ in 0 .. line_count {
            vec.push(read_line!($delimiter; $ty));
        }
        vec
    }};
    ($count: expr; $ty: ty) => {{
        let line_count = $count;
        let mut vec: Vec<$ty> = Vec::with_capacity(line_count);
        for _ in 0 .. line_count {
            vec.push(read_value!());
        }
        vec
    }}
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
#[derive(Clone)]
struct Bit {
    vec: Vec<i64>
}
const MOD: i64 = 1000000007i64;
impl Bit {
    fn new(size: usize) -> Bit {
        Bit{vec: vec![0i64; size]}
    }
    fn add(&mut self, mut pos: usize, value: i64) {
        while pos < self.vec.len() {
            self.vec[pos] += value;
            self.vec[pos] %= MOD;
            pos += !pos & (pos + 1);
        }
    }
    fn get(&self, mut pos: usize) -> i64 {
        let mut result = 0i64;
        let mut pos = pos as i32;
        while pos >= 0 {
            result += self.vec[pos as usize];
            result %= MOD;
            pos -= !pos & (pos + 1);
        }
        result
    }
}
struct LcmSegmentTree {
    vec: Vec<Vec<i64>>
}
fn gcd(a: i64, b: i64) -> i64 {
    match b {
        0 => a,
        _ => gcd(b, a % b),
    }
}
fn lcm(a: i64, b: i64) -> i64 {
    a / gcd(a, b) * b
}
impl LcmSegmentTree {
    fn new(initial : Vec<i64>) -> LcmSegmentTree {
        let mut len = initial.len();
        let mut vec = vec![initial];
        while len > 1 {
            vec.push(vec![1i64; len / 2]);
            len /= 2;
        }
        for i in 1 .. vec.len() {
            for j in 0 .. vec[i - 1].len() {
                if j / 2 < vec[i].len() {
                    vec[i][j / 2] = lcm(vec[i][j / 2], vec[i - 1][j]);
                }
            }
        }
        LcmSegmentTree{vec: vec}
    }
    fn lcm(&self, mut from: usize, mut to: usize) -> i64 {
        let mut result = 1i64;
        let mut from = from as i32;
        let mut to = to as i32;
        for v in &self.vec {
            if from % 2 == 1 {
                result = lcm(result, v[from as usize]);
                from += 1;
            }
            if to % 2 == 0 {
                result = lcm(result, v[to as usize]);
                to -= 1;
            }
            if to < from {
                break;
            }
            from /= 2;
            to /= 2;
        }
        result
    }
}
fn main(){
    const MAX_LOOP: usize = 40;
    let_all!(n: usize, q: usize);
    let permutations: Vec<usize> = read_line!(' '; usize).iter().map(|x| x - 1).collect();
    let mut loop_length = vec![MAX_LOOP + 1; n];
    let mut loop_sum = vec![0i64; n];
    let mut bits = vec![Bit::new(n); MAX_LOOP + 1];
    for i in 0 .. n {
        if loop_length[i] == MAX_LOOP + 1 {
            let mut length = 1;
            let mut sum = (i + 1) as i64;
            let mut current = permutations[i];
            while current != i {
                length += 1;
                sum += (current + 1) as i64;
                sum %= MOD;
                current = permutations[current];
            }
            loop_length[i] = length;
            loop_sum[i] = sum;
            current = permutations[i];
            while current != i {
                loop_length[current] = length;
                loop_sum[current] = sum;
                current = permutations[current];
            }
        }
        bits[loop_length[i]].add(i, loop_sum[i]);
    }
    let bits = bits;
    let lcm_tree = LcmSegmentTree::new(loop_length.iter().map(|&x| x as i64).collect());
    for _ in 0 .. q {
        let_all!(l: usize, r: usize);
        let l = l - 1;
        let r = r - 1;
        let loop_length = lcm_tree.lcm(l, r);
        let mut result = 0i64;
        for i in 1 .. MAX_LOOP + 1 {
            result += bits[i].get(r) * ((loop_length / i as i64) % MOD) % MOD;
            if l > 0 {
                result -= bits[i].get((l - 1) as usize) * ((loop_length / i as i64) % MOD) % MOD;
            }
            result = (result % MOD + MOD) % MOD;
        }
        println!("{}", result);
    }
}


Measured memory_bytes: 51884