// ---------- begin karatsuba multiplication ----------
fn karatsuba<T>(a: &[T], b: &[T], c: &mut [T], zero: T, buf: &mut [T])
where
    T: std::marker::Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>,
{
    assert!(a.len() == b.len());
    let n = a.len();
    if n <= 16 {
        for (i, a) in a.iter().enumerate() {
            for (c, b) in c[i..].iter_mut().zip(b.iter()) {
                *c = *c + *a * *b;
            }
        }
        return;
    }
    if n & 1 == 1 {
        karatsuba(&a[1..], &b[1..], &mut c[2..], zero, buf);
        let x = a[0];
        let y = b[0];
        c[0] = c[0] + x * y;
        for (c, (a, b)) in c[1..].iter_mut().zip(a[1..].iter().zip(b[1..].iter())) {
            *c = *c + x * *b + *a * y;
        }
        return;
    }
    let m = n / 2;
    let (fa, ta) = a.split_at(m);
    let (fb, tb) = b.split_at(m);
    karatsuba(fa, fb, &mut c[..n], zero, buf);
    karatsuba(ta, tb, &mut c[n..], zero, buf);
    let (x, buf) = buf.split_at_mut(m);
    let (y, buf) = buf.split_at_mut(m);
    let (z, buf) = buf.split_at_mut(n);
    for z in z.iter_mut() {
        *z = zero;
    }
    for (x, (p, q)) in x.iter_mut().zip(fa.iter().zip(ta.iter())) {
        *x = *p + *q;
    }
    for (y, (p, q)) in y.iter_mut().zip(fb.iter().zip(tb.iter())) {
        *y = *p + *q;
    }
    karatsuba(x, y, z, zero, buf);
    for (z, (p, q)) in z.iter_mut().zip(c[..n].iter().zip(c[n..].iter())) {
        *z = *z - (*p + *q);
    }
    for (c, z) in c[m..].iter_mut().zip(z.iter()) {
        *c = *c + *z;
    }
}

fn multiply<T>(a: &[T], b: &[T], zero: T) -> Vec<T>
where
    T: std::marker::Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>,
{
    let mut ans = vec![zero; a.len() + b.len()];
    {
        let mut ans = ans.as_mut_slice();
        let mut a = a;
        let mut b = b;
        let mut buf = vec![zero; 4 * std::cmp::min(a.len(), b.len())];
        let mut c = Vec::with_capacity(2 * std::cmp::min(a.len(), b.len()));
        while !(a.is_empty() || b.is_empty()) {
            if a.len() <= b.len() {
                let len = a.len();
                for x in b.chunks_exact(len) {
                    c.clear();
                    c.resize(2 * len, zero);
                    karatsuba(a, x, &mut c, zero, &mut buf);
                    for (ans, c) in ans.iter_mut().zip(c.iter()) {
                        *ans = *ans + *c;
                    }
                    ans = ans.split_at_mut(len).1;
                    b = b.split_at(len).1;
                }
            } else {
                let len = b.len();
                for x in a.chunks_exact(len) {
                    c.clear();
                    c.resize(2 * len, zero);
                    karatsuba(x, b, &mut c, zero, &mut buf);
                    for (ans, c) in ans.iter_mut().zip(c.iter()) {
                        *ans = *ans + *c;
                    }
                    ans = ans.split_at_mut(len).1;
                    a = a.split_at(len).1;
                }
            }
        }
    }
    ans.truncate(a.len() + b.len() - 1);
    ans
}
// ---------- end karatsuba multiplication ----------
// ---------- begin ModInt ----------
const MOD: u32 = 1_000_000_007;

#[derive(Clone, Copy)]
struct ModInt(u32);

impl std::ops::Add for ModInt {
    type Output = ModInt;
    fn add(self, rhs: ModInt) -> Self::Output {
        let mut d = self.0 + rhs.0;
        if d >= MOD {
            d -= MOD;
        }
        ModInt(d)
    }
}

impl std::ops::AddAssign for ModInt {
    fn add_assign(&mut self, rhs: ModInt) {
        *self = *self + rhs;
    }
}

impl std::ops::Sub for ModInt {
    type Output = ModInt;
    fn sub(self, rhs: ModInt) -> Self::Output {
        let mut d = self.0 + MOD - rhs.0;
        if d >= MOD {
            d -= MOD;
        }
        ModInt(d)
    }
}

impl std::ops::SubAssign for ModInt {
    fn sub_assign(&mut self, rhs: ModInt) {
        *self = *self - rhs;
    }
}

impl std::ops::Mul for ModInt {
    type Output = ModInt;
    fn mul(self, rhs: ModInt) -> Self::Output {
        ModInt((self.0 as u64 * rhs.0 as u64 % MOD as u64) as u32)
    }
}

impl std::ops::MulAssign for ModInt {
    fn mul_assign(&mut self, rhs: ModInt) {
        *self = *self * rhs;
    }
}

impl std::ops::Neg for ModInt {
    type Output = ModInt;
    fn neg(self) -> Self::Output {
        ModInt(if self.0 == 0 { 0 } else { MOD - self.0 })
    }
}

impl std::fmt::Display for ModInt {
    fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::str::FromStr for ModInt {
    type Err = std::num::ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let val = s.parse::<u32>()?;
        Ok(ModInt::new(val))
    }
}

impl From<usize> for ModInt {
    fn from(val: usize) -> ModInt {
        ModInt((val % MOD as usize) as u32)
    }
}

#[allow(dead_code)]
impl ModInt {
    pub fn new(n: u32) -> ModInt {
        ModInt(n % MOD)
    }
    pub fn zero() -> ModInt {
        ModInt(0)
    }
    pub fn one() -> ModInt {
        ModInt(1)
    }
    pub fn pow(self, mut n: u32) -> ModInt {
        let mut t = ModInt::one();
        let mut s = self;
        while n > 0 {
            if n & 1 == 1 {
                t *= s;
            }
            s *= s;
            n >>= 1;
        }
        t
    }
    pub fn inv(self) -> ModInt {
        assert!(self.0 > 0);
        self.pow(MOD - 2)
    }
}
// ---------- end ModInt ----------
// ---------- begin Precalc ----------
#[allow(dead_code)]
struct Precalc {
    inv: Vec<ModInt>,
    fact: Vec<ModInt>,
    ifact: Vec<ModInt>,
}

#[allow(dead_code)]
impl Precalc {
    pub fn new(n: usize) -> Precalc {
        let mut inv = vec![ModInt::one(); n + 1];
        let mut fact = vec![ModInt::one(); n + 1];
        let mut ifact = vec![ModInt::one(); n + 1];
        for i in 2..(n + 1) {
            inv[i] = -inv[MOD as usize % i] * ModInt(MOD / i as u32);
            fact[i] = fact[i - 1] * ModInt(i as u32);
            ifact[i] = ifact[i - 1] * inv[i];
        }
        Precalc {
            inv: inv,
            fact: fact,
            ifact: ifact,
        }
    }
    pub fn inv(&self, n: usize) -> ModInt {
        self.inv[n]
    }
    pub fn fact(&self, n: usize) -> ModInt {
        self.fact[n]
    }
    pub fn ifact(&self, n: usize) -> ModInt {
        self.ifact[n]
    }
    pub fn comb(&self, n: usize, k: usize) -> ModInt {
        if k > n {
            return ModInt::zero();
        }
        self.fact[n] * self.ifact[k] * self.ifact[n - k]
    }
}
// ---------- end Precalc ----------
//---------- begin union_find ----------
#[allow(dead_code)]
mod union_find {
    pub struct UF {
        p: Vec<i32>,
    }
    impl UF {
        pub fn new(n: usize) -> UF {
            UF { p: vec![-1; n] }
        }
        pub fn init(&mut self) {
            for p in self.p.iter_mut() {
                *p = -1;
            }
        }
        pub fn root(&self, mut x: usize) -> usize {
            while self.p[x] >= 0 {
                x = self.p[x] as usize;
            }
            x
        }
        pub fn same(&self, x: usize, y: usize) -> bool {
            self.root(x) == self.root(y)
        }
        pub fn unite(&mut self, mut x: usize, mut y: usize) -> Option<(usize, usize)> {
            x = self.root(x);
            y = self.root(y);
            if x == y {
                return None;
            }
            if self.p[x] > self.p[y] {
                let s = x;
                x = y;
                y = s;
            }
            self.p[x] += self.p[y];
            self.p[y] = x as i32;
            Some((x, y))
        }
        pub fn parent(&self, x: usize) -> Option<usize> {
            let p = self.p[x];
            if p >= 0 {
                Some(p as usize)
            } else {
                None
            }
        }
        pub fn get_size(&self, x: usize) -> usize {
            let r = self.root(x);
            (-self.p[r]) as usize
        }
    }
}
//---------- end union_find ----------

use proconio::*;

fn run() {
    input! {
        n: usize,
        p: [i32; n],
    }
    let mut elem = vec![false; n];
    let mut u = union_find::UF::new(n);
    for (i, &p) in p.iter().enumerate() {
        if p < 0 {
            let r = u.root(i);
            elem[r] = true;
        } else {
            let p = p as usize - 1;
            if let Some((a, b)) = u.unite(i, p) {
                elem[a] |= elem[b];
            }
        }
    }
    let mut fix = 0;
    let mut cnt = vec![0; n + 1];
    let mut a = 0;
    for i in 0..n {
        if i == u.root(i) {
            fix += u.get_size(i) - 1;
            if elem[i] {
                a += 1;
                cnt[u.get_size(i)] += 1;
            }
        }
    }
    let mut dp = vec![ModInt::one()];
    let pc = Precalc::new(n);
    for (i, c) in cnt.iter().enumerate().skip(1).filter(|c| *c.1 > 0).rev() {
        let mut p = vec![ModInt::zero(); *c + 1];
        let mut mul = ModInt::one();
        let rad = ModInt::from(i);
        for (j, p) in p.iter_mut().enumerate() {
            *p = pc.comb(*c, j) * mul;
            mul *= rad;
        }
        dp = multiply(&dp, &p, ModInt::zero());
    }
    let rad = ModInt::from(n - 1);
    let mut ans = ModInt::from(fix + a) * ModInt::from(n - 1).pow(a as u32);
    let mut mul = ModInt::one();
    for (i, &v) in dp.iter().enumerate().skip(1).rev() {
        if i == 1 {
            ans -= (v - ModInt::from(a)) * mul;
        } else {
            ans -= pc.fact(i - 1) * v * mul;
        }
        mul *= rad;
    }
    println!("{}", ans);
}

fn main() {
    run();
}


Measured memory_bytes: 2400