Method Resolution Order(快速幂)

Zotlin is an object-oriented programming language that provides features such as Classes and Functions. Zotlin also has the following features:

  • A function with the same name and same declarations can have different implementations in different classes.
  • Each class can inherit zero or more classes (excluding itself). If Class A inherits Class B, we say that Class A is a child of Class B.
  • A class cannot inherit another class if a cycle is formed by inheritance.

 

For example, if Class A inherits Class B and Class B inherits Class C, then Class C cannot inherit Class A.

A Class X is called an ancestor of Class Y, if Y inherits directly or indirectly from Class X. ie, say Class Y inherits from Class Z, which inherits from Class X. Then Class X is an ancestor of both Class Y and Class Z.

 

Mr. Zourist — a very popular programmer — wrote a program in Zotlin with N classes numbered from 1 to N. Some of these classes have an implementation of a function F. The Zotlin compiler needs to determine which definition of function F should be called for an object of Class 1.

In order to deterministically decide which implementation of function F to use, the compiler uses a list called the Resolution Order (RO list). This is a list containing Class 1 and its ancestors. The compiler iterates through this list to search for a class that contains an implementation of function F.

The properties of an RO list are as follows:

  • The 1st member of the list is Class 1.
  • The list contains only the ancestors of Class 1. All the Classes in the list are unique.
  • It is guaranteed that no Class occurs after any of its ancestors in the list.

 

Let's define the Special RO list as an RO list of length N where the ith member in the list is Class i. That is, the Special RO list is [1, 2, 3, ... , N].

Find out the number of distinct class-inheritance hierarchies Mr. Zourist could have created in his program for which one of the valid RO lists is the Special RO list. Print your output modulo 109 + 7.

Two class-inheritance hierarchies are considered different if they have at least one class that inherits a different list of classes.

Input

The first line of input contains a positive integer T - denoting the total number of testcases.

T lines follow - the ith line contains a single positive integer N- denoting the number of classes written by Mr. Zourist.

 

Output

For each testcase, print the required number of distinct class-inheritance hierarchies modulo 109 + 7 on a new line.

 

Constraints

  • 1 ≤ T ≤ 105
  • 1 ≤ N ≤ 105

Example

Input

2
3
6

Output
3
9765

 

Explanation

Explanation for testcase 1:

Let "class X" denote that X does not inherit any class

Let "class X(Y)" denote that X inherits Y

Let "class X(Y, Z)" denote that X inherits Y, Z

 

First possibility:

class 2
class 3
class 1(2, 3)

 

Second possibility:

class 3
class 1(2)
class 2(3)

 

Third possibility:

class 3
class 1(2, 3)
class 2(3)

 

These are the three valid class-inheritance hierarchies, and hence the answer is 3.

Note that the following hierarchy is not valid:

        class 1(2)
        class 2
        class 3
    

This is because 3 should have been an ancestor of 1, but in the above hierarchy, 3 is a stand-alone class.

#include <iostream>
#include <cstdio>
using namespace std;
const int inf=1e9+7;
long long ksm(int n)
{
    long long a=2,b=1;
    while(n)
    {
        if(n%2)
        {
            b*=a;
            b%=inf;
        }
        a*=a;
        a%=inf;
        n/=2;
    }
    return b-1;
}
int main()
{
    int t,n,i;
    long long a[100050];
    a[1]=1;
    for(int i=2; i<=100000; i++)
    {
        a[i]=a[i-1]*ksm(i-1);
        a[i]%=inf;
    }
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%lld\n",a[n]);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值