import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] s; int n = Integer.parseInt(reader.readLine()); long[] m = new long[n]; s = reader.readLine().split(" "); for (int m_i = 0; m_i < n; m_i++) { m[m_i] = Integer.parseInt(s[m_i]); } long[][] f = new long[n + 1][n + 1]; for (int i = 1; i <= n; i++) { f[i][i] = i; for (int j = i + 1; j <= n; j++) { f[i][j] = (f[i][j - 1] * j) % 1_000_000_007L; } } int max = n; long sum = 1; for (int i = 0; i < n - 1; i++) { if (m[i] > m[i + 1]) { max = i + 1; break; } } if (max == n && n > 1) { sum++; } long[][] d = new long[n + 1][n + 1]; for (int i = 0; i <= n; i++) { d[1][i] = 1; d[i][n] = 1; d[i][n - 1] = i; } long a; int w; for (int i = 2; i <= max; i++) { w = 1; for (int j = n - 2; j >= i * 2; j--) { if (m[j] < m[j + 1]) { w++; } else { w = 1; } a = 0; for (int k = 1, q = Math.min(w, Math.min(i, n - j)); k <= q; k++) { a = (a + d[k][j + k] * f[i - k + 1][i]) % 1_000_000_007L; } d[i][j] = a; } } for (int i = 2; i <= max; i++) { a = 0; for (int k = 1; k <= Math.min(n - i, i); k++) { a = (a + d[k][i + k] * f[i - k + 1][i]) % 1_000_000_007L; if (!(i + k < n && m[i + k - 1] < m[i + k])) { break; } } sum = (sum + a) % 1_000_000_007L; } System.out.println(sum); } }