using System; using System.Diagnostics; class Solution { private const int MOD = 1000000007; static void Main(String[] args) { Precalculate(); //Console.SetIn(new StringReader("1\r\n1")); // 1 //Console.SetIn(new StringReader("2\r\n1 2")); // 2 //Console.SetIn(new StringReader("3\r\n1 2 3")); // 4 //Console.SetIn(new StringReader("4\r\n1 2 3 4")); // 9 // Unconfirmed //Console.SetIn(new StringReader("5\r\n1 2 3 4 5")); // 21 //Console.SetIn(new StringReader("6\r\n1 2 3 4 5 6")); // 60 //Console.SetIn(new StringReader("7\r\n1 2 3 4 5 6 7")); // 174 //Console.SetIn(new StringReader("10\r\n1 2 3 4 5 6 7 8 9 10")); // 8862 //Console.SetIn(new StringReader("2\r\n2 1")); // 1 //Console.SetIn(new StringReader("3\r\n3 2 1")); // 1 //Console.SetIn(new StringReader("4\r\n4 3 2 1")); // 1 //Console.SetIn(new StringReader("3\r\n1 3 2")); // 3 //Console.SetIn(new StringReader("3\r\n2 3 1")); // 3 //Console.SetIn(new StringReader("3\r\n2 1 3")); // 1 //Console.SetIn(new StringReader("3\r\n3 1 2")); // 1 //Console.SetIn(new StringReader("4\r\n1 3 2 4")); // 5 //Console.SetIn(new StringReader("5\r\n1 2 4 3 5")); // 12 //Console.SetIn(new StringReader("4\r\n2 3 4 1")); // 6 //Console.SetIn(new StringReader("6\r\n4 5 6 1 2 3")); // 24 //int lim = 1200; //string input = string.Format("{0}\r\n{1}", lim, string.Join(" ", Enumerable.Range(1, lim))); //Console.SetIn(new StringReader(input)); Solve(); } private static void Solve() { int n = Convert.ToInt32(Console.ReadLine()); string[] m_temp = Console.ReadLine().Split(' '); int[] m = Array.ConvertAll(m_temp, Int32.Parse); // your code goes here long[,] dp = new long[n, n]; dp[0, 0] = 1; for (int i = 1; i < n; i++) { for (int j = i; j >= 0; j--) { if (j < i && m[j] > m[j + 1]) break; int len = i - j + 1; long sum = 0; for (int k = j - len; k >= 0; k--) { if (k < j - 1 && m[k] > m[k + 1]) break; int len2 = (j - 1) - k + 1; sum += dp[k, j - 1] * Comb(len, len2); sum = sum % MOD; } if (len > i) sum = 1; dp[j, i] = sum; Debug.Assert(dp[j, i] >= 0); } } long ans = 0; for (int i = 0; i < n; i++) { ans += dp[i, n - 1]; ans = ans % MOD; } Console.WriteLine(ans); } static long[,] combs = new long[1200, 1200]; private static void Precalculate() { for (int j = 0; j < 1200; j++) { combs[0, j] = j+1; } for (int i = 1; i < 1200; i++) { for (int j = i; j < 1200; j++) { combs[i, j] = combs[i - 1, j] * (j-i+1) % MOD; } } } private static long Comb(int len, int len2) { return combs[len-1, len2-1]; } }