Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

Separate String

You are given a string $t$ and a set $S$ of $N$ different strings. You need to separate $t$ such that each part is included in $S$.

For example, the following 4 separation methods satisfy the condition when $t = abab$ and $S = \{a, ab, b\}$.

$a,b,a,b$

$a,b,ab$

$ab,a,b$

$ab,ab$

Your task is to count the number of ways to separate $t$. Because the result can be large, you should output the remainder divided by $1,000,000,007$.

Input

The input consists of a single test case formatted as follows.

$N$
$s_1$
:
$s_N$
$t$

The first line consists of an integer $N$ ($1 \leq N \leq 100,000$) which is the number of the elements of $S$. The following $N$ lines consist of $N$ distinct strings separated by line breaks. The $i$-th string $s_i$ represents the $i$-th element of $S$. $s_i$ consists of lowercase letters and the length is between $1$ and $100,000$, inclusive. The summation of length of $s_i$ ($1 \leq i \leq N$) is at most $200,000$. The next line consists of a string $t$ which consists of lowercase letters and represents the string to be separated and the length is between $1$ and $100,000$, inclusive.

Output

Calculate the number of ways to separate $t$ and print the remainder divided by $1,000,000,007$.

Sample Input 1

3
a
b
ab
abab

Output for Sample Input 1

4

Sample Input 2

3
a
b
c
xyz

Output for Sample Input 2

0

Sample Input 3

7
abc
ab
bc
a
b
c
aa
aaabcbccababbc

Output for Sample Input 3

160

Sample Input 4

10
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Output for Sample Input 4

461695029

Solution in C++14 (judge verdict: Accepted)

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
    out << "{" << p.first << ", " << p.second << "}";
    return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010


// int main(){
// 
// 
//   return 0;
// }

/* Rolling Hash */
// n = 9973, 10007

struct RollingHash{
  int N, base;
  ll hash[SIZE], pow[SIZE];

  template <typename Type>
  RollingHash(int base, int n, Type *arr):
    N(n), base(base)
  {
    hash[0] = 0;
    pow[0] = 1;
    for(int i=0; i<N; i++){
      hash[i+1] = (hash[i] + arr[i]) * base % mod;
      pow[i+1] = pow[i] * base % mod;
    }
  }

  // get [l,r)
  ll getHash(int l, int r) {
    return (mod + hash[r] - hash[l] * pow[r-l] % mod) % mod;
  }
};

const int base1 = 9973;
const int base2 = 10007;

set<pair<ll,ll>> ss[700];
map<int,int> dict;
int dicti = 0;
vector<int> vec;

ll getHash(int base, int n, char *arr){
  RollingHash rh(base, n, arr);
  return rh.getHash(0, n);
}

char s[SIZE], t[SIZE];
ll dp[SIZE] = {};

int main(){
  int n;

  scanf("%d", &n);

  for(int i=0;i<n;i++){
    scanf("%s", s);
    int m = strlen(s);
    ll h1 = getHash(base1, m, s);
    ll h2 = getHash(base2, m, s);
    vec.push_back(m);
    if(dict.find(m) == dict.end()) dict[m] = dicti++;
    ss[dict[m]].insert({h1, h2});
  }

  scanf("%s", t);

  int m = strlen(t);
  RollingHash rh1(base1, m, t), rh2(base2, m, t);

  dp[0] = 1;

  for(int i=0;i<m;i++){
    for(auto it : dict){
      int p = it.first;
      if(i+p > m) break;
      ll h1 = rh1.getHash(i, i+p);
      ll h2 = rh2.getHash(i, i+p);
      if(ss[it.second].find({h1, h2}) != ss[it.second].end())
        dp[i+p] = (dp[i+p] + dp[i]) % mod;
    }
  }

  printf("%lld\n", dp[m]);

  return 0;
}