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;
inline void boostIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
}
int Mod = 1e9 + 7;
int inf = 2e9;
const int Y = 1;
struct Hash {
  string s;
  int n;
  vector<int> H;
  vector<int> P;
  int K;
  int p;
  Hash(string& z) {
    K = 1e9 + 7;
    p = 29;
    s = z;
    n = z.size();
    H.assign(z.size() + 1, 0);
    P.assign(z.size() + 1, 0);
    H[0] = 1;
    P[0] = 1;
    for (int i = 1; i <= n; ++i) {
      H[i] = (1ll * H[i - 1] * p + (s[i - 1] + 1)) % K;
      P[i] = 1ll * P[i - 1] * p % K;
    }
  }
  int Get(int l, int r) {
    int v;
    v = (H[r + 1] - 1ll * H[l] * P[r - l + 1] % K + K);
    if (v >= K) v -= K;
    return v;
  }
  int GetWH(int l, int r, int a) {
    int aa;
    int bb;
    aa = Get(l, a - 1);
    bb = Get(a + 1, r);
    int sizea = ((a - 1) - l + 1);
    int sizeb = (r - (a + 1) + 1);
    int v;
    int g = P[sizeb];
    v = (1ll * aa * g + bb) % K;
    return v;
  }
};
char SYM(string& s, Hash& h, int a, int m) {
  char asym;
  if (m < a)
    asym = s[m];
  else
    asym = s[m + 1];
  return asym;
}
int HSWL(string& s, Hash& h, int a, int m) {
  if (m < a) {
    return h.Get(0, m);
  } else {
    if (a != 0) {
      return h.GetWH(0, m + 1, a);
    } else {
      return h.Get(1, m + 1);
    }
  }
}
char CWL(string& s, Hash& h, int a, string& s2, Hash& h2, int a2) {
  int n = (int)min(s.size(), s2.size()) - 1;
  int l = -1;
  int r = n;
  while (r - l > 1) {
    int m = (r + l) / 2;
    if (HSWL(s, h, a, m) == HSWL(s2, h2, a2, m)) {
      l = m;
    } else {
      r = m;
    }
  }
  int s_size = s.size();
  int s2_size = s2.size();
  if (a < s.size()) s_size--;
  if (a2 < s2.size()) s2_size--;
  if ((r + 1 == s_size || r + 1 == s2_size) &&
      SYM(s, h, a, r) == SYM(s2, h2, a2, r)) {
    return s_size <= s2_size;
  }
  return SYM(s, h, a, r) <= SYM(s2, h2, a2, r);
}
vector<int> Sort(string& s, Hash& h, vector<char>& nxt) {
  int n = s.size() + 1;
  vector<int> A(n);
  int l = 0;
  int r = 0;
  for (int i = n - 2; i >= 0; --i) {
    if (s[i] > nxt[i]) {
      --l;
      A[i] = l;
    } else {
      ++r;
      A[i] = r;
    }
  }
  int Min = inf;
  for (int i = 0; i < n; ++i) {
    Min = min(Min, A[i]);
  }
  for (int i = 0; i < n; ++i) {
    A[i] += (-Min);
  }
  vector<int> Order(n);
  for (int i = 0; i < n; ++i) {
    Order[A[i]] = i;
  }
  return Order;
}
template <typename T>
inline T getint() {
  T val = 0;
  char c;
  bool neg = false;
  while ((c = getchar()) && !(c >= '0' && c <= '9')) {
    neg |= c == '-';
  }
  do {
    val = (val * 10) + c - '0';
  } while ((c = getchar()) && (c >= '0' && c <= '9'));
  return val * (neg ? -1 : 1);
}
inline void getstring(string& s) {
  char c;
  bool neg = false;
  c = getchar();
  do {
    s.push_back(c);
  } while ((c = getchar()) && (c >= 'a' && c <= 'z'));
}
int32_t main() {
  boostIO();
  int n;
  n = getint<int>();
  vector<string> S(n + 1);
  vector<vector<int>> dp(n + 1);
  vector<vector<char>> nxt(n + 1);
  vector<Hash> H;
  for (int i = 0; i <= n; ++i) {
    if (i != 0)
      getstring(S[i]);
    else {
      S[i].push_back('#');
    }
    vector<int> temp(S[i].size() + 1);
    vector<char> temp2(S[i].size());
    dp[i] = temp;
    nxt[i] = temp2;
    H.push_back(Hash(S[i]));
  }
  for (int i = 0; i <= n; ++i) {
    char x1, x2, x3;
    x3 = '#';
    x2 = S[i][S[i].size() - 1];
    nxt[i][S[i].size() - 1] = x3;
    for (int j = (int)S[i].size() - 2; j >= 0; --j) {
      x1 = S[i][j];
      if (x1 == x2) {
        x2 = x3;
      }
      nxt[i][j] = x2;
      x3 = x2;
      x2 = x1;
    }
  }
  vector<vector<int>> ML(n + 1);
  for (int i = 0; i <= n; ++i) {
    ML[i] = Sort(S[i], H[i], nxt[i]);
  }
  int g = 1;
  int sum = 0;
  int prsum = 1;
  for (int i = 1; i <= n; ++i) {
    int p = ML[i - 1].size() - 1;
    int c = ML[i].size() - 1;
    for (c; c >= 0; --c) {
      while (p >= 0 &&
             !CWL(S[i - 1], H[i - 1], ML[i - 1][p], S[i], H[i], ML[i][c])) {
        prsum = (prsum - dp[i - 1][p] + Mod);
        if (prsum >= Mod) prsum -= Mod;
        --p;
      }
      dp[i][c] = (prsum);
      sum = (sum + dp[i][c]);
      if (sum >= Mod) sum -= Mod;
    }
    prsum = sum;
    sum = 0;
  }
  cout << prsum << '\n';
  cerr << '\n' << "RUNTIME: " << 1.0 * clock() / CLOCKS_PER_SEC << " s" << '\n';
  return 0;
  ;
}