Gym - 102394I Interesting Permutation (思维)

DreamGrid has an interesting permutation of 1,2,…,n1,2,…,n denoted by a1,a2,…,ana1,a2,…,an. He generates three sequences ff, gg and hh, all of length nn, according to the permutation aa in the way described below:

  • For each 1≤i≤n1≤i≤n, fi=max{a1,a2,…,ai}fi=max{a1,a2,…,ai};
  • For each 1≤i≤n1≤i≤n, gi=min{a1,a2,…,ai}gi=min{a1,a2,…,ai};
  • For each 1≤i≤n1≤i≤n, hi=fi−gihi=fi−gi.

BaoBao has just found the sequence hh DreamGrid generates and decides to restore the original permutation. Given the sequence hh, please help BaoBao calculate the number of different permutations that can generate the sequence hh. As the answer may be quite large, print the answer modulo 109+7109+7.

Input

The input contains multiple cases. The first line of the input contains a single integer TT (1≤T≤200001≤T≤20000), the number of cases.

For each case, the first line of the input contains a single integer nn (1≤n≤1051≤n≤105), the length of the permutation as well as the sequences. The second line contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤i≤n,0≤hi≤1091≤i≤n,0≤hi≤109).

It's guaranteed that the sum of nn over all cases does not exceed 2⋅1062⋅106.

Output

For each case, print a single line containing a single integer, the number of different permutations that can generate the given sequence hh. Don't forget to print the answer modulo 109+7109+7.

Example

Input

3
3
0 2 2
3
0 1 2
3
0 2 3

Output

2
4
0

Note

For the first sample case, permutations {1,3,2}{1,3,2} and {3,1,2}{3,1,2} can both generate the given sequence.

For the second sample case, permutations {1,2,3}{1,2,3}, {2,1,3}{2,1,3}, {2,3,1}{2,3,1} and {3,2,1}{3,2,1} can generate the given sequence.

题意:h[i] = a[1]~a[i]中的最大值 - 最小值,给定 h,求有多少种排列

思路:h[i] < h[i - 1] || h[1] != 0 || h[i] > n - 1时排列不存在。cnt计数不影响h的位置(空位),即不会放当前的最大值或最小值,考虑h[i - 1]和h[i],若h[i] == h[i - 1],说明a[i]不是最大值也不是最小值,可以从当前的空位中选一个,即ans *= cnt, cnt--;若h[i] > h[i - 1],说明a[i]是最大值或最小值,两种选择情况,产生h[i] - h[i - 1] - 1个空位,ans *= 2, cnt += h[i] - h[i - 1] - 1。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int N = 1e5 + 10;

ll h[N];

int main() {
    int n, t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        bool flag = 1;
        for(int i = 1; i <= n; ++i) {
            scanf("%lld", &h[i]);
            if(h[i] > n - 1) flag = 0;
            if(i > 1 && h[i] < h[i - 1]) flag = 0;
            if(i == 1 && h[i] != 0) flag = 0;
        }
        if(!flag) {
            printf("0\n");
            continue;
        }
        ll ans = 1, cnt = 0;
        for(int i = 2; i <= n; ++i) {
            if(h[i] == h[i - 1]) {
                ans = ans * cnt % mod;
                cnt = (cnt - 1 + mod) % mod;
            }
            else {
                ans = ans * 2 % mod;
                cnt = (cnt + h[i] - h[i - 1] - 1) % mod;
            }
        }
        printf("%lld\n", ans % mod);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值