#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define elif else if
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int,int>
#define repname(a, b, c, d, e, ...) e
#define rep(...) repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define rep0(x) for (int rep_counter = 0; rep_counter < (x); ++rep_counter)
#define rep1(i, x) for (int i = 0; i < (x); ++i)
#define rep2(i, l, r) for (int i = (l); i < (r); ++i)
#define rep3(i, l, r, c) for (int i = (l); i < (r); i += (c))
struct ScalarInput {
template<class T>
operator T(){
T ret;
cin >> ret;
return ret;
}
};
struct VectorInput {
size_t n;
VectorInput(size_t n): n(n) {}
template<class T>
operator vector<T>(){
vector<T> ret(n);
for(T &x : ret) cin >> x;
return ret;
}
};
ScalarInput input(){ return ScalarInput(); }
VectorInput input(size_t n){ return VectorInput(n); }
template<typename T>
void print(vector<T> a){
for(int i=0;i<a.size();i++){
cout<<a[i]<<" \n"[i+1==a.size()];
}
}
template<class T>
void print(T x){
cout << x << '\n';
}
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail){
cout << head << ' ';
print(forward<Tail>(tail)...);
}
using u64=uint64_t;
inline static u64 a = 12345;
u64 next() {
u64 x = a;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return a = x;
}
#include <atcoder/segtree>
#include <atcoder/modint>
using namespace atcoder;
using mint=modint1000000007;
int op(int a,int b){
return max(a,b);
}
int e(){
return -1;
}
int m=1e6+100;
int N;
segtree<int,op,e>seg;
vector<int>A;
vector<vector<int>>edge;
vector<ll>H(m),cumH(m+1);
void calc(int l,int r){
//cerr<<"calc "<<l<<" "<<r<<endl;
if(l+1==r){
edge[l].push_back(r);
return;
}
int mid=(l+r)/2;
ll hash=0;
set<int>S;
map<ll,vector<int>>mp1,mp2;
for(int i=mid-1;i>=l;i--){
int now=A[i];
while(S.find(now)!=S.end()){
S.erase(now);
hash^=H[now];
seg.set(now,now);
now++;
}
hash^=H[now];
S.insert(now);
seg.set(now,-1);
int top_bit=*rbegin(S);
int low_bit=*begin(S);
int con_bit=seg.prod(0,top_bit)+1;
mp1[hash].push_back(i);
ll hash2=hash^(cumH[top_bit+1]^cumH[con_bit+1]);
mp2[hash2].push_back(i);
}
for(auto i:S){
seg.set(i,i);
}
S.clear();
hash=0;
for(int i=mid;i<r;i++){
int now=A[i];
while(S.find(now)!=S.end()){
S.erase(now);
hash^=H[now];
now++;
}
hash^=H[now];
S.insert(now);
int top_bit=*rbegin(S);
int low_bit=*begin(S);
ll rev_hash=hash^(cumH[top_bit+1]^cumH[low_bit])^H[low_bit];
for(auto j:mp1[rev_hash]){
edge[j].push_back(i+1);
}
ll rev_hash2=rev_hash^H[top_bit+1];
if(top_bit==low_bit)rev_hash2=hash;
for(auto j:mp2[rev_hash2]){
edge[j].push_back(i+1);
}
}
calc(l,mid);
calc(mid,r);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
rep(i,m)H[i]=next();
cumH[0]=0;
rep(i,m)cumH[i+1]=cumH[i]^H[i];
vector<int>init(m);
rep(i,m)init[i]=i;
seg=segtree<int,op,e>(init);
cin>>N;
A.resize(N);
rep(i,N){
cin>>A[i];
A[i]++;
}
edge.resize(N);
calc(0,N);
vector<mint>dp(N+1,0);
dp[0]=1;
rep(l,N){
sort(edge[l].begin(),edge[l].end());
int tmp=-1;
for(auto r:edge[l]){
if(r!=tmp)dp[r]+=dp[l];
tmp=r;
}
}
print(dp[N].val());
}