Problem: This is a harder version of the problem E with larger constraints.

Twilight Sparkle has received a new task from Princess Celestia. This time she asked to decipher the ancient scroll containing important knowledge of pony origin.

To hide the crucial information from evil eyes, pony elders cast a spell on the scroll. That spell adds exactly one letter in any place to each word it is cast on. To make the path to the knowledge more tangled elders chose some of words in the scroll and cast a spell on them.

Twilight Sparkle knows that the elders admired the order in all things so the scroll original scroll contained words in lexicographically non-decreasing order. She is asked to delete one letter from some of the words of the scroll (to undo the spell) to get some version of the original scroll. 

Unfortunately, there may be more than one way to recover the ancient scroll. To not let the important knowledge slip by Twilight has to look through all variants of the original scroll and find the required one. To estimate the maximum time Twilight may spend on the work she needs to know the number of variants she has to look through. She asks you to find that number! Since that number can be very big, Twilight asks you to find it modulo 10^9+7.

It may occur that princess Celestia has sent a wrong scroll so the answer may not exist.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

  * a is a prefix of b, but a ≠ b;
  * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5): the number of words in the scroll.

The i-th of the next n lines contains a string consisting of lowercase English letters: the i-th word in the scroll. The length of each word is at least one. The sum of lengths of words does not exceed 10^6.

Output

Print one integer: the number of ways to get a version of the original from the scroll modulo 10^9+7.

Examples

Input


3
abcd
zaza
ataka


Output


4


Input


4
dfs
bfs
sms
mms


Output


8


Input


3
abc
bcd
a


Output


0


Input


6
lapochka
kartyshka
bigbabytape
morgenshtern
ssshhhiiittt
queen


Output


2028

Note

Notice that the elders could have written an empty word (but they surely cast a spell on it so it holds a length 1 now).

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const unsigned long long int seed = 50009uLL;
string s[(100010)];
vector<unsigned long long int> h[(100010)];
long long int cnt[(100010)];
unsigned long long int xp[(1000010)];
void init_xp(int n) {
  xp[0] = 1;
  for (int i = 1; i < n; i++) xp[i] = xp[i - 1] * seed;
}
void init_hash(int k) {
  string &s_ = s[k];
  h[k].clear();
  h[k].push_back(s_[0] - 'a' + 1);
  for (int i = 1, sz = s_.size(); i < sz; i++) {
    h[k].push_back(h[k][i - 1] * seed +
                   (unsigned long long int)(s_[i] - 'a' + 1));
  }
}
unsigned long long int ask_hash(int k, int l, int r) {
  if (l > r) return 0;
  if (!l) return h[k][r];
  return h[k][r] - h[k][l - 1] * xp[r - l + 1];
}
unsigned long long int cal_hash(int k, int x, int n) {
  if (-1 == x || x > n) {
    return ask_hash(k, 0, n);
  }
  n++;
  return ask_hash(k, 0, x - 1) * xp[n - x] + ask_hash(k, x + 1, n);
}
struct node {
  int i, j;
  char nxt;
  long long int t;
  bool operator<(const node &o) const {
    string &a = s[i], &b = s[o.i];
    int lena = a.size() - int(j > -1), lenb = b.size() - int(o.j > -1);
    int mx = min(lena, lenb) - 1;
    int l = 0, r = mx, k = -1;
    while (l <= r) {
      int m = (l + r) >> 1;
      unsigned long long int ha = cal_hash(i, j, m), hb = cal_hash(o.i, o.j, m);
      if (ha == hb) {
        k = m;
        l = m + 1;
      } else {
        r = m - 1;
      }
    }
    if (mx == k) {
      if (lena == lenb) return i < o.i;
      return lena < lenb;
    }
    char ca = (j != -1 && j <= k + 1) ? a[k + 2] : a[k + 1];
    char cb = (o.j != -1 && o.j <= k + 1) ? b[k + 2] : b[k + 1];
    return ca < cb;
  }
  bool operator>=(const node &o) const { return o < *this; }
};
vector<node> buf, v[(100010)];
list<node> v1, v2;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  init_xp((1000010));
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    init_hash(i);
    s[i].push_back('$');
    buf.clear();
    for (int j = 0, t = 0, sz = s[i].size(); j < sz; j++) {
      if (!j || s[i][j] == s[i][j - 1])
        t++;
      else {
        buf.push_back((node){i, j - 1, s[i][j], t});
        t = 1;
      }
    }
    s[i].pop_back();
    v1.clear();
    v2.clear();
    for (int j = 0, sz = buf.size(); j < sz; j++) {
      node &o = buf[j];
      if (o.nxt < s[i][o.j] || o.nxt == '$') {
        v1.push_back(buf[j]);
      } else {
        v2.push_front(buf[j]);
      }
    }
    for (list<node>::iterator it = v1.begin(); it != v1.end(); it++) {
      v[i].push_back(*it);
    }
    v[i].push_back((node){i, -1, '\0', 1});
    for (list<node>::iterator it = v2.begin(); it != v2.end(); it++) {
      v[i].push_back(*it);
    }
  }
  if (1 == n) {
    cout << s[1].size() + 1 << endl;
    return 0;
  }
  for (int i = 2; i <= n; i++) {
    long long int tot = 0;
    for (int j = 0, k = 0, sz1 = v[i].size(), sz2 = v[i - 1].size(); j < sz1;
         j++) {
      while (k < sz2 && v[i][j] >= v[i - 1][k]) {
        tot += v[i - 1][k].t;
        tot %= (1000000007);
        k++;
      }
      v[i][j].t *= tot;
      v[i][j].t %= (1000000007);
    }
  }
  long long int ans = 0;
  for (int i = 0, sz = v[n].size(); i < sz; i++) {
    ans += v[n][i].t;
    ans %= (1000000007);
  }
  cout << ans << endl;
  return 0;
}