Automorphisms

No attempts yetTime limit3sMemory limit128 MB

Problem

A tournament is a directed graph in which:

  • for every two distinct vertices uu and vv there is exactly one edge between them (either uvu \to v or vuv \to u),
  • there are no loops (for every vertex uu there is no edge uuu \to u).

Let pp be a permutation of the tournament's vertices. (A permutation of a finite set XX is a bijection from XX to XX.) The permutation pp is an automorphism if for every two distinct vertices uu and vv the edge between uu and vv points in the same direction as the edge between p(u)p(u) and p(v)p(v); that is, uvu \to v is an edge if and only if p(u)p(v)p(u) \to p(v) is an edge. For a given permutation pp we want to know how many tournaments have pp as an automorphism.

For example, take the vertex set {1,,4}\{1, \dots, 4\} and the permutation pp with p(1)=2p(1)=2, p(2)=4p(2)=4, p(3)=3p(3)=3, p(4)=1p(4)=1. There are exactly four tournaments for which this permutation is an automorphism:

Four tournaments on four vertices

Write a program that:

  • reads the description of a permutation of an nn-element set from standard input,
  • computes tt, the number of distinct nn-vertex tournaments for which this permutation is an automorphism,
  • writes to standard output the remainder of tt divided by 10001000.

Input

The first line contains one integer nn (1n100001 \le n \le 10000), the number of vertices. The vertices are numbered from 11 to nn. Each of the next nn lines contains one integer: line k+1k+1 holds p(k)p(k), the value of the permutation at vertex kk.

Output

Print one integer: the remainder of tt divided by 10001000, where tt is the number of distinct nn-vertex tournaments for which pp is an automorphism.