#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e6 + 10;
const int mod=1e9+7;
typedef long long ll;
ll tt[maxn];
struct node{
char s[maxn];
int Lyndon_end[maxn];
int coun;
void get_Lyndon()
{
coun=0;
int N = strlen(s), j, k ,len=1;
for(int i = 0; i < N;) {
j = i; k = i + 1;
len=1;
tt[i]=i;
while(k <= N && s[j] <= s[k]) {
if(s[j] < s[k]){//代表循环扩张
j = i;
len=k-i+1;
tt[k]=i;
}
else{
j++;//代表循环接着了
tt[k]=len+ tt[k-len];
}
k++;
}
while(i <= j) {
Lyndon_end[coun++]=i + k - j - 1;
i += k - j;
}
}
}
};
int main() {
int T;
scanf("%d",&T);
node Ly;
while (T--) {
scanf("%s",Ly.s);
int len=strlen(Ly.s);
Ly.get_Lyndon();
ll ans=0,t=1;
for(int i=0;i<len;i++)
{
//cout<<tt[i]<<endl;
ans=(ans+t*(tt[i]+1))%mod;
t=t*1112%mod;
}
printf("%lld\n",ans);
}
return 0;
}