#include <bits/stdc++.h>          
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
    for (in_vec_cout res : vec) {cout << res << " ";}
    cout << endl;
}

const ll inf = 1e18;
const ll mod = 1e9 + 7;

ll H, W;
vector<string> S;
pair<ll,ll> st, en;
map<char,vector<pair<ll,ll>>> T;
map<char,bool> seen;

vector<ll> dx = {1,0,-1,0};
vector<ll> dy = {0,1,0,-1};

void init() {
    cin >> H >> W;
    S.resize(H);
    rep(i,H) cin >> S[i];
    rep(i,H) rep(j,W) {
        char c = S[i][j];
        pair<ll,ll> p = {i,j};
        if (c=='.' || c=='#') continue;
        if (c=='S') st = p;
        else if (c=='G') en = p;
        else T[c].push_back(p);
    }
}

ll solve() {
    vector<vector<ll>> dist(H,vector<ll>(W,inf));
    dist[st.first][st.second] = 0;
    queue<pair<ll,ll>> que;
    que.push(st);
    while (que.size()) {
        pair<ll,ll> p = que.front();
        que.pop();
        ll x = p.first, y = p.second;
        ll d = dist[x][y];
        rep(i,4) {
            ll nx = x + dx[i], ny = y + dy[i];
            if (nx<0 || nx>=H || ny<0 || ny>=W) continue;
            if (S[nx][ny]=='#') continue;
            ll nd = d + 1;
            if (nd<dist[nx][ny]) {
                dist[nx][ny] = nd;
                que.push({nx,ny});
            }
        }
        char c = S[x][y];
        if (c=='.' || c=='S' || c=='G') continue;
        if (seen[c]) continue;
        seen[c] = true;
        for (auto p : T[c]) {
            ll nx = p.first, ny = p.second;
            ll nd = d + 1;
            if (dist[nx][ny]>nd) {
                dist[nx][ny] = nd;
                que.push(p);
            }
        }
    }
    ll res = dist[en.first][en.second];
    if (res==inf) res = -1;
    return res;
}

int main() {
    init();
    ll ans = solve();
    Cout(ans);
}
///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           nd(a,b,c)                   mp(mp(a,b),c)
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=2e18;
const int mod=1e9+7;
const int M=3005;

inline ll bigmod(ll B,ll P){ll R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;}
inline ll ad(ll x,ll y){ll ret=(x%mod+y%mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline ll sub(ll x,ll y){ll ret=((x%mod)-(y%mod)+mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline ll gun(ll x,ll y){ll ret=((x%mod)*(y%mod))%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}

int n;
int a[M],pre[M];

int dp[M][M];

int sum[M],nxt[M];

 main()

{
    fast
    cin>>n;
    f(i,1,n)cin>>a[i],pre[i]=a[i]+pre[i-1];

    rf(k,n,1)
    {
        f(pos,1,n)
        {
            int val=pre[pos-1]%k;

            sum[val]=sub(sum[val],dp[pos][k+1]);

            dp[pos][k]=sum[val];

            int z=pre[n]-pre[pos-1];
            z=z%k;

            if(z==0)dp[pos][k]=(dp[pos][k]+1)%mod;

            if(k!=1){

            val=pre[pos-1]%(k-1);
            nxt[val]=ad(nxt[val],dp[pos][k]);

            }
        }

        f(j,0,k+1)
        {
            sum[j]=nxt[j];
            nxt[j]=0;
        }
    }

    cout<<dp[1][1]<<"\n";
    return 0;

}



































