import java.util.Scanner; public class Solution { static final int MOD = 1000000007; /* 5 1 1 = 1 x x x-1 x-2 x-3 */ static int mult(int a, int b) { long longRes = (long) a * b; longRes %= MOD; return (int) longRes; } static int sum(int a, int b) { return (a + b) % MOD; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] m = new int[n]; boolean[][] sortDp = new boolean[n][n]; for(int i = 0; i < n; i++) { m[i] = in.nextInt(); } for (int l = 0; l < n; l++) { sortDp[l][l] = true; for (int r = l + 1; r < n; r++) { sortDp[l][r] = sortDp[l][r - 1] && m[r] > m[r - 1]; } } int[][] cdp = new int[n + 1][n + 1]; for (int i = 1; i <= n; i++) { cdp[i][i] = i; } for (int len = 2; len <= n; len++) { for (int from = 1; from + len - 1 <= n; from++) { cdp[from][from + len - 1] = mult(cdp[from][from + len - 2], (from + len - 1)); } } int dp[][] = new int[n + 1][n + 1]; for (int i = 1; i <= n; i++) { dp[i][i] = sortDp[0][i - 1] ? 1 : 0; } // dp[1][1] = 1; // for (int passed = 1; passed <= n; passed++) { // for (int curCnt = 1; curCnt <= passed; curCnt++) { // for (int prevCnt = curCnt; prevCnt <= passed - curCnt; prevCnt++) { // dp[passed][curCnt] += mult(dp[passed - curCnt][prevCnt], cdp[prevCnt - curCnt + 1][curCnt]); // dp[passed][curCnt] %= MOD; // } // } // } for (int passed = 1; passed <= n; passed++) { for (int curCnt = Math.min(n, passed); curCnt >= 1; curCnt--) { if (dp[passed][curCnt] == 0) { continue; } for (int nextCnt = 1; nextCnt <= curCnt && passed + nextCnt <= n; nextCnt++) { if (!sortDp[passed][passed + nextCnt - 1]) { break; } dp[passed + nextCnt][nextCnt] = sum(dp[passed + nextCnt][nextCnt], mult(dp[passed][curCnt], cdp[curCnt - nextCnt + 1][curCnt])); } } } int res = 0; for (int i = 1; i <= n; i++) { res = sum(res, dp[n][i]); } System.out.println(res); } }