import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class A { static final int mod = (int)1e9 + 7; static int[] fac, comb[], memo[]; static int N; static boolean[][] sorted; static int dp(int n, int k) { if(k == 0 || n == 0) return k + n == 0 ? 1 : 0; if(k > n || !sorted[N - n][N + k - n - 1]) return 0; if(memo[n][k] != -1) return memo[n][k]; int ret = 0; for(int i = 0; i <= k; ++i) { ret += 1l * dp(n - k, k - i) * fac[k - i] % mod * comb[k][i] % mod; ret %= mod; } return memo[n][k] = ret; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(), m[] = new int[n]; for(int i = 0; i < n; ++i) m[i] = sc.nextInt(); N = n; sorted = new boolean[n][n]; for(int i = 0; i < n; ++i) { int last = m[i]; for(int j = i; j < n && m[j] >= last; ++j) { sorted[i][j] = true; last = m[j]; } } fac = new int[n + 1]; fac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = (int)(1l * i * fac[i - 1] % mod); comb = new int[n + 1][n + 1]; comb[0][0] = 1; for(int i = 1; i <= n; ++i) { comb[i][0] = 1; for(int j = 1; j <= i; ++j) comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % mod; } memo = new int[n + 1][n + 1]; for(int[] x: memo) Arrays.fill(x, -1); int ans = 0; for(int i = 1; i <= n; ++i) ans = (ans + dp(n, i)) % mod; out.println(ans); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException {return br.ready();} } }