#include using namespace std; #define FOR(i,a,b) for (int i = a; i < b; ++i) #define RFOR(i,a,b) for(int i = a; i >= b;--i) #define REP(i, n) FOR(i, 0, n) #define f first #define s second #define ll long long #define pb push_back #define mp make_pair #define pii pair #define sz(f) (int)f.size() #define vi vector < int > #define db(x) cerr << #x << " = " << x << endl #define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; #define y1 jaksflkajsf #define pii pair #define pli pair const int inf = 1e9; const int N = 1e5+5; const double PI = acos(-1.0); const int T = 920000; const int MOD = 1000000000+7; string s; int add; int n,cnt[55]; int fact[N]; int rfact[N]; int mPow(int x, int n) { if(n==0) return 1; if(n&1) return 1LL*x*mPow(x,n-1)%MOD; int t=mPow(x,n/2); return 1LL*t*t%MOD; } int C(int n, int k) { int ret=1LL*fact[n]*rfact[k]%MOD; ret=1LL*ret*rfact[n-k]%MOD; return ret; } bool equals(vi &a,const vi &b) { if(sz(a)!=sz(b)) return false; REP(i,sz(a)) if(a[i]!=b[i]) return false; return true; } map was; bool can(vi &a, const vi &b) { REP(i,sz(a)) FOR(j,i+1,sz(a)) { if(a[i]==a[j])continue; swap(a[i],a[j]); if(equals(a,b)) { swap(a[i],a[j]); return true; } FOR(c1,i+1,sz(a)) FOR(c2,c1+1,sz(a)) { if(a[c1]==a[c2]) continue; swap(a[c1],a[c2]); if(equals(a,b)) { swap(a[c1],a[c2]); swap(a[i],a[j]); return true; } swap(a[c1],a[c2]); } swap(a[i],a[j]); } return false; } int w3[27][27][27]; int w4[27][27][27][27]; int calcFour(int c1, int c2, int c3, int c4) { if(c1==c2 && c1==c3 && c1==c4) return 0; if(w4[c1][c2][c3][c4]!=-1) return w4[c1][c2][c3][c4]; w4[c1][c2][c3][c4]=0; vi p; int ret=0; p.pb(c1); p.pb(c2); p.pb(c3); p.pb(c4); bool ok=true; REP(i,sz(p)) FOR(j,i+1,sz(p)) if(p[i]!=p[j]) { ok=false; break; } if(ok) return 0; vi t=p; sort(p.begin(),p.end()); do{ bool ok=true; REP(i,sz(t)) if(t[i]==p[i]) ok=false; if(ok && can(t,p)) { ret++; } }while(next_permutation(p.begin(),p.end())); w4[c1][c2][c3][c4]=ret; return ret; } int calcTriple(int c1, int c2, int c3) { if (w3[c1][c2][c3]!=-1) return w3[c1][c2][c3]; w3[c1][c2][c3]=0; if(c1==c2 && c2==c3 && c1==c3) return 0; vi p; int ret=0; p.pb(c1); p.pb(c2); p.pb(c3); sort(p.begin(),p.end()); do{ if (c1==p[0] || c2==p[1] || c3==p[2]) {} else { vi t; t.pb(c1); t.pb(c2); t.pb(c3); if (can(t,p)){ ret++; } } }while(next_permutation(p.begin(),p.end())); w3[c1][c2][c3]=ret; return ret; } void solve() { cin>>s; int ans=0; n=sz(s); REP(c,26) cnt[c]=0; REP(i,n){ cnt[s[i]-'a']++; } int F3=1LL*C(n,3)*fact[n-3]%MOD; int F4=1LL*C(n,4)*fact[n-4]%MOD; REP(c1,26){ if(cnt[c1]==0) continue; cnt[c1]--; REP(c2,26) { if(cnt[c2]==0) continue; cnt[c2]--; if (c1!=c2) { add=C(n,2); add=1LL*add*fact[n-2]%MOD; REP(c,26) if (cnt[c]>1) add=1LL*add*mPow(fact[cnt[c]], MOD-2)%MOD; ans=(ans+add)%MOD; } int f=1; REP(i,26)f=1LL*f*rfact[cnt[i]]%MOD; REP(c3,26) { if(cnt[c3]==0) continue; int ft=1LL*f*fact[cnt[c3]]%MOD; cnt[c3]--; ft=1LL*ft*rfact[cnt[c3]]%MOD; int ret=calcTriple(c1,c2,c3); ret=1LL*ret*F3%MOD; ret=1LL*ret*ft%MOD; ans=(ans+ret)%MOD; REP(c4, 26) { if(cnt[c4]==0) continue; int ft2=1LL*ft*fact[cnt[c4]]%MOD; cnt[c4]--; int ret=calcFour(c1,c2,c3,c4); ret=1LL*ret*F4%MOD; ft2=1LL*ft2*rfact[cnt[c4]]%MOD; ret=1LL*ret*ft2%MOD; ans=(ans+ret)%MOD; cnt[c4]++; } cnt[c3]++; } cnt[c2]++; } cnt[c1]++; } int res=fact[n]; REP(i,26) res=1LL*res*mPow(fact[cnt[i]], MOD-2)%MOD; ans=(ans+res)%MOD; res=1LL*res*res%MOD; res=res-ans+MOD; res%=MOD; cout<>t; REP(it,t)solve(); return 0; }