#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

#ifndef INCLUDED_UNION_FIND_HPP
#define INCLUDED_UNION_FIND_HPP

#include <cstdint>
#include <vector>

class union_find {
public:
	using size_type = std::int_fast32_t;
	using container = std::vector<size_type>;
	
	container data;
	size_type comp_cnt;

private:
	const size_type find(size_type k) {
		if(data[k] < 0) return k;
		return data[k] = find(data[k]);
	}

public:
	union_find() = default;
	union_find(const union_find &) = default;
	union_find(union_find&&) = default;

	union_find & operator=(const union_find &) = default;
	union_find & operator=(union_find &&) = default;

	union_find(size_type N): data(N, -1), comp_cnt(N) {}

	bool unite(size_type x, size_type y) {
		x = find(x); y = find(y);
		if(x == y) return false;
		if(data[x] > data[y]) std::swap(x, y);
		
		data[x] += data[y];
		data[y] = x;
		comp_cnt--;
		return true;
	}
	
	bool is_united(size_type x, size_type y) { return find(x) == find(y); }
	size_type size() const { return data.size(); }
	size_type size(size_type k) { return -data[find(k)]; }
	size_type comp(size_type k) { return find(k); };
	size_type count() const { return comp_cnt; }
	size_type operator[](size_type k) { return find(k); }
	
	void swap(union_find & r) {
		data.swap(r.data);
		std::swap(comp_cnt, r.comp_cnt);
	}
};

#endif

#ifndef INCLUDED_MODINT_HPP
#define INCLUDED_MODINT_HPP

#include <iostream>

template <std::uint_fast64_t Modulus>
class modint {
	using u32 = std::uint_fast32_t;
	using u64 = std::uint_fast64_t;
	using i64 = std::int_fast64_t;

	inline u64 apply(i64 x) { return (x < 0 ? x + Modulus : x); };

public:
	u64 a;
	static constexpr u64 mod = Modulus;

	constexpr modint(const i64& x = 0) noexcept: a(apply(x % (i64)Modulus)) {}

	constexpr modint operator+(const modint& rhs) const noexcept { return modint(*this) += rhs; }
	constexpr modint operator-(const modint& rhs) const noexcept { return modint(*this) -= rhs; }	
	constexpr modint operator*(const modint& rhs) const noexcept { return modint(*this) *= rhs; }
	constexpr modint operator/(const modint& rhs) const noexcept { return modint(*this) /= rhs; }
	constexpr modint operator^(const u64& k) const noexcept { return modint(*this) ^= k; }
	constexpr modint operator^(const modint& k) const noexcept { return modint(*this) ^= k.value(); }
	constexpr modint operator-() const noexcept { return modint(Modulus - a); }
	constexpr modint operator++() noexcept { return (*this) = modint(*this) + 1; }
	constexpr modint operator--() noexcept { return (*this) = modint(*this) - 1; }
	const bool operator==(const modint& rhs) const noexcept { return a == rhs.a; };
	const bool operator!=(const modint& rhs) const noexcept { return a != rhs.a; };
	const bool operator<=(const modint& rhs) const noexcept { return a <= rhs.a; };
	const bool operator>=(const modint& rhs) const noexcept { return a >= rhs.a; };
	const bool operator<(const modint& rhs) const noexcept { return a < rhs.a; };
	const bool operator>(const modint& rhs) const noexcept { return a > rhs.a; };
	constexpr modint& operator+=(const modint& rhs) noexcept {
		a += rhs.a;
		if (a >= Modulus) a -= Modulus;
		return *this;
	}
	constexpr modint& operator-=(const modint& rhs) noexcept {
		if (a < rhs.a) a += Modulus;
		a -= rhs.a;
		return *this;
	}
	constexpr modint& operator*=(const modint& rhs) noexcept {
		a = a * rhs.a % Modulus;
		return *this;
	}
	constexpr modint& operator/=(modint rhs) noexcept {
		u64 exp = Modulus - 2;
		while (exp) {
			if (exp % 2) (*this) *= rhs;
			
			rhs *= rhs;
			exp /= 2;
		}
		return *this;
	}
	constexpr modint& operator^=(u64 k) noexcept {
		auto b = modint(1);
		while(k) {
			if(k & 1) b = b * (*this);
			(*this) *= (*this);
			k >>= 1;
		}
		return (*this) = b;
	}
	constexpr modint& operator=(const modint& rhs) noexcept {
		a = rhs.a;
		return (*this);
	}

	constexpr u64& value() noexcept { return a; }
	constexpr const u64& value() const noexcept { return a; }
	explicit operator bool() const { return a; }
	explicit operator u32() const { return a; }

	const modint inverse() const {
		return modint(1) / *this;
	}
	const modint pow(i64 k) const {
		return modint(*this) ^ k;
	}

	friend std::ostream& operator<<(std::ostream& os, const modint& p) {
		return os << p.a;
	}
	friend std::istream& operator>>(std::istream& is, modint& p) {
		u64 t;
		is >> t;
		p = modint(t);
		return is;
	}
};

#endif
using mint = modint<1000000007>;

int main() {
	int n; scanf("%d", &n); std::vector<int> p(n);
	for(auto& v: p) {
		scanf("%d", &v);
		if(v != -1) v--;
	}

	i64 cnt = 0;
	for(auto v: p) cnt += (v == -1);

	union_find uf(n);
	for(int i = 0; i < n; i++) if(p[i] != -1) uf.unite(i, p[i]);

	std::vector<int> vec;
	for(int i = 0; i < n; i++) vec.push_back(uf[i]);
	sort(begin(vec), end(vec));
	vec.erase(unique(begin(vec), end(vec)), end(vec));

	std::vector<int> size(n), exist(n);
	for(int i = 0; i < n; i++) {
		size[uf[i]]  += 1;
		exist[uf[i]] += (p[i] == -1);
	}
	for(auto v: exist) assert(v == 0 or v == 1);

	std::vector<mint> fact(n + 1, 1);
	for(int i = 0; i < n; i++) fact[i + 1] = fact[i] * mint(i + 1);

	mint ans = 0;
	for(auto id: vec) ans += (size[id] - 1); ans *= mint(n - 1).pow(cnt);
	for(int I = 0; I < vec.size(); I++) {
		for(int J = I + 1; J < vec.size(); J++) {
			int i = vec[I], j = vec[J];
			
			if(exist[i] and exist[j]) {
				mint all = mint(n - 1).pow(cnt) - mint(n - 1).pow(cnt - 2) * mint(n - size[i] - 1) * mint(n - size[j] - 1);
	
				ans += all;
			} else if(!exist[i] and exist[j]) {
				ans += mint(size[i]) * mint(n - 1).pow(cnt - 1);
			} else if(exist[i] and !exist[j]) {
				ans += mint(size[j]) * mint(n - 1).pow(cnt - 1);
			} else {
				continue;
			}
		}
	}

	auto dp = make_v<mint>(n + 1); dp[0] = 1;
	for(int i: vec) {
		if(!exist[i]) continue;

		auto to(dp);
		for(int j = 0; j < n; j++) to[j + 1] += dp[j] * size[i];
		dp.swap(to);
	}
	for(int i = 3; i < n + 1; i++) ans -= dp[i] * fact[i - 1] * mint(n - 1).pow(cnt - i);
		
	printf("%lld\n", ans.value());
	return 0;
}


Measured memory_bytes: 3992