output_description: For each way to make requests, find the minimum number of roads needed to meet
  all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      P_1 P_2 \cdots P_N
attempt: import unittest
  
  
  class UnionFind:
  
      def __init__(self, size: int):
          # 負の値はルート (集合の代表) で集合の個数
          # 正の値は次の要素を表す
          self.size = size
          self.parent = [-1] * size
  
      def find(self, x: int) -> int:
          """
          xを含む集合の代表を求める
          """
          if self.parent[x] < 0:
              return x
          # 集合の代表にリンクを繋ぎ変える
          same_group_items = []
          while self.parent[x] >= 0:
              same_group_items.append(x)
              x = self.parent[x]
          for child in same_group_items:
              self.parent[child] = x
          return x
  
      def unite(self, x: int, y: int):
          """
          xを含む集合とyを含む集合を併合する
          """
          root_x = self.find(x)
          root_y = self.find(y)
          if root_x != root_y:
              if self.parent[root_x] >= self.parent[root_y]:
                  self.parent[root_x] += self.parent[root_y]
                  self.parent[root_y] = root_x
              else:
                  self.parent[root_y] += self.parent[root_x]
                  self.parent[root_x] = root_y
  
      def same(self, x: int, y: int) -> bool:
          return self.find(x) == self.find(y)
  
      def count_group(self) -> int:
          """
          集合の数を数える。
          """
          count = 0
          for i in range(self.size):
              if self.parent[i] < 0:
                  count += 1
          return count
  
      def get_size(self, x):
          return -1 * self.parent[self.find(x)]
  
  
  def solve(n, ps):
      mod = 10**9 + 7
      undefs = []
      uf = UnionFind(n)
      base_cost = 0
      for i in range(n):
          if ps[i] == -1:
              undefs.append(i)
          else:
              p = ps[i] - 1
              if not uf.same(p, i):
                  base_cost += 1
                  uf.unite(p, i)
      n_undef = len(undefs)
      n_undef_pattern_p = pow(n - 1, n_undef - 2, mod)
      n_undef_pattern = n_undef_pattern_p * (n - 1) * (n - 1) % mod
      base_cost = base_cost * n_undef_pattern
  
      cost = 0
      for i, p in enumerate(undefs):
          m = uf.get_size(p) - 1
          cost += (n - 1 - m) * n_undef_pattern_p * (n - 1)
          cost -= i * n_undef_pattern_p * (n - 2)
          cost %= mod
  
      # print(base_cost, cost, n_undef, n_undef_pattern)
  
      return (base_cost + cost) % mod
  
  
  def main():
      n = int(input())
      ps = list(map(int, input().split()))
      result = solve(n, ps)
      print(result)
  
  
  class Test(unittest.TestCase):
      def test_solve_1(self):
          n = 4
          p = [2, 1, -1, 3]
          actual = solve(n, p)
          expected = 8
          self.assertEqual(actual, expected)
  
      def test_solve_2(self):
          n = 2
          p = [2, 1]
          actual = solve(n, p)
          expected = 1
          self.assertEqual(actual, expected)
  
      def test_solve_3(self):
          n = 10
          p = [2, 6, 9, -1, 6, 9, -1, -1, -1, -1]
          actual = solve(n, p)
          expected = 527841
          self.assertEqual(actual, expected)
  
      def test_solve_4(self):
          n = 2
          p = [2, -1]
          actual = solve(n, p)
          expected = 1
          self.assertEqual(actual, expected)
  
      def test_solve_5(self):
          n = 2
          p = [-1, -1]
          actual = solve(n, p)
          expected = 1
          self.assertEqual(actual, expected)
  
      def test_solve_6(self):
          n = 3
          p = [-1, -1, -1]
          actual = solve(n, p)
          expected = 16
          self.assertEqual(actual, expected)
  
      def test_solve_7(self):
          n = 4
          p = [-1, -1, -1, -1]
          actual = solve(n, p)
          expected = 240
          self.assertEqual(actual, expected)
  
      def test_solve_8(self):
          n = 4
          p = [2, -1, -1, -1]
          actual = solve(n, p)
          expected = 80
          self.assertEqual(actual, expected)
  
  
  if __name__ == "__main__":
      main()
  
problem_description: Statement
  
  There are N towns numbered 1, 2, \cdots, N.
  
  Some roads are planned to be built so that each of them connects two distinct
  towns bidirectionally. Currently, there are no roads connecting towns.
  
  In the planning of construction, each town chooses one town different from
  itself and requests the following: roads are built so that the chosen town is
  reachable from itself using one or more roads.
  
  These requests from the towns are represented by an array P_1, P_2, \cdots,
  P_N. If P_i = -1, it means that Town i has not chosen the request; if 1 \leq
  P_i \leq N, it means that Town i has chosen Town P_i.
  
  Let K be the number of towns i such that P_i = -1. There are (N-1)^K ways in
  which the towns can make the requests. For each way to make requests, find the
  minimum number of roads needed to meet all the requests, and print the sum of
  those (N-1)^K numbers, modulo (10^9+7).
samples: [{"input": "4\n    2 1 -1 3", "output": "8\n    \n\nThere are three ways to make requests, as follows:\n\n  * Choose P_1 = 2, P_2 = 1, P_3 = 1, P_4 = 3. In this case, at least three roads - for example, (1,2),(1,3),(3,4) \\- are needed to meet the requests.\n  * Choose P_1 = 2, P_2 = 1, P_3 = 2, P_4 = 3. In this case, at least three roads - for example, (1,2),(1,3),(3,4) \\- are needed to meet the requests.\n  * Choose P_1 = 2, P_2 = 1, P_3 = 4, P_4 = 3. In this case, at least two roads - for example, (1,2),(3,4) \\- are needed to meet the requests.\n\nNote that it is not mandatory to connect Town i and Town P_i directly.\n\nThe sum of the above numbers is 8.\n\n* * *"}, {"input": "2\n    2 1", "output": "1\n    \n\nThere may be just one fixed way to make requests.\n\n* * *"}, {"input": "10\n    2 6 9 -1 6 9 -1 -1 -1 -1", "output": "527841"}]