1013: 湖南省第十七届大学生计算机程序设计竞赛(HNCPC2021) - Semilive

开始 Start
0000-00-0000:00:00UTC+0
结束 End
0000-00-0000:00:00UTC+0
当前 Now
0000-00-0000:00:00UTC+0
状态 Status
加载中Loading
类型 Type
开放Public
榜单 Rank
加载中Loading
公告 Announcement
暂无公告No announcement

E : Not the Only Tree

时间限制Time Limit
1 秒Sec
内存限制Memory Limit
128 兆MB
提交次数Submitted
44 次Times
通过次数Solved
8 次Times

题目描述Description

We all know that to build a traditional “Binary Search Tree”, we put the smaller numbers to the left while the larger to the right. Different data insertion order may result in different tree structure.

In this problem, you need to calculate how many different input sequences there are to get a binary search tree with a specific structure.

输入格式Input

There are no more than 100 test cases. Each case contains two lines:

The first line is an integer n.

The second line is a permutation of [1, n], describing a Binary Search Tree constructed according to the order of the sequence.

1 ≤ n ≤ 103.

输出格式Output

For each test case, output the number of different sequences which could build a Binary Search Tree with the same structure, including the input one.

The result should be modulo 109 + 7.

样例Sample

提示Hint

For the first case, 3 2 1 4, 3 2 4 1, 3 4 2 1 could make the same tree:

    3
   / \
  2   4
 /
1

So the answer is 3.