// what is matter? never mind.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define int unsigned long long
#define ull unsigned long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 1000000007
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 10005
#define inf 0x3f3f3f3f
int n;
char s[maxn];
int sum[maxn][7];
modint w[maxn][7];
modint g1[8][8][8],g2[8][8][8],res;
int cnt[maxn][7];
void wk(modint g1[8][8][8]){
For(a,0,6)For(b,0,6)For(c,0,6){
g1[7][b][c]+=g1[a][b][c];
g1[a][7][c]+=g1[a][b][c];
g1[a][b][7]+=g1[a][b][c];
g1[7][7][c]+=g1[a][b][c];
g1[a][7][7]+=g1[a][b][c];
g1[7][b][7]+=g1[a][b][c];
g1[7][7][7]+=g1[a][b][c];
}
}
void solve(int pos)
{
// s[pos]=0 next .. pre ??
// cout<<"solve "<<pos<<"\n";
assert(pos>0);
memset(g1,0,sizeof g1);
memset(g2,0,sizeof g2);
For(i,0,6) For(j,0,6) For(k,0,6) {
int a=(i+sum[pos+1][0])%7,b=(j+sum[pos+1][2])%7,c=(k+sum[pos+1][4])%7;
modint coe=1;
coe*=w[cnt[pos-1][0]-1][(i-1+7)%7];
coe*=w[cnt[pos-1][2]][j];
coe*=w[cnt[pos-1][4]][k];
// if(coe.x) cout<<"add1 "<<i<<" "<<j<<" "<<k<<" "<<coe.x<<"\n";
g1[(a+b+c)%7][(a+4*b+2*c)%7][(a+2*b+4*c)%7]+=coe;
}
For(i,0,6) For(j,0,6) For(k,0,6) {
int a=(i+sum[pos+1][1])%7,b=(j+sum[pos+1][3])%7,c=(k+sum[pos+1][5])%7;
modint coe=1;
coe*=w[cnt[pos-1][1]][i];
coe*=w[cnt[pos-1][3]][j];
coe*=w[cnt[pos-1][5]][k];
// if(coe.x) cout<<"add2 "<<i<<" "<<j<<" "<<k<<" "<<coe.x<<"\n";
g2[(a+b+c)%7][(2*a+b+4*c)%7][(3*a+6*b+5*c)%7]+=coe;
}
// For(a,0,6)For(b,0,6)For(c,0,6)For(d,0,6)For(e,0,6)For(f,0,6)
// if(a!=d && (a+d)%7!=0 && (b!=e) && (b+e)%7!=0 && (c!=f) && (c+f)%7!=0)
// if(g1[a][b][c].x && g2[d][e][f].x)
// // cout<<"add "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g1[a][b][c].x<<" "<<g2[d][e][f].x<<"\n",
// res+=g1[a][b][c]*g2[d][e][f];
//cout<<"res "<<res.x<<"\n";
wk(g1),wk(g2);
For(a,0,6) For(b,0,6) For(c,0,6) {
modint t=0;
vi oa={a,7-a,7}; if(a==0)oa={0,7};
vi ob={b,7-b,7}; if(b==0)ob={0,7};
vi oc={c,7-c,7}; if(c==0)oc={0,7};
for(int xa:oa)for(int xb:ob)for(int xc:oc){
modint now=g2[xa][xb][xc];
if(xa!=7)now=-now;
if(xb!=7)now=-now;
if(xc!=7)now=-now;
t+=now;
}
res+=g1[a][b][c]*t;
}
}
void work()
{
cin>>s;
n=strlen(s);
reverse(s,s+n);
res=0;
int pp=0;
while(pp<n && s[pp]=='1')s[pp]='0',++pp;
if(pp>=n)++n,s[n-1]='1';
else s[pp]='1';
// For(i,0,n-1)cout<<s[i];puts(" s");
For(i,0,6) sum[n][i]=sum[n+1][i]=0;
Rep(i,n-1,0){
For(j,0,6) sum[i][j]=sum[i+1][j];
if(s[i]&1) sum[i][i%6]++;
}
For(i,1,n-1){
if(s[i]&1){
solve(i);
}
}
cout<<res.x<<"\n";
}
signed main()
{
For(i,0,10000){
if(i){
For(j,0,6)cnt[i][j]=cnt[i-1][j];
}
cnt[i][i%6]+=1;
}
For(i,0,10000){
For(j,0,i){
w[i][j%7]+=C(i,j);
}
}
int T=read();
while(T--)work();
return 0;
}
/*
*/