1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <bits/stdc++.h>
using namespace std;

int n;

int m=1;
vector <pair <int,int> > wek[1000007];
vector <pair <int,int> > wek2[1000007];
int dos[1000007];

int r;
int num;

vector <int> co[1000007];
int po[1000007];

long long dp[1000007];
long long mod=1000000007;

int wsk[1000007];
int nast[1000007][2];

inline int tab(int v, int u)
{
	u-=wek[v][0].first;
	if (u<0 || u>=(int)wek[v].size())
		return -1;
	return wek[v][u].second;
}

inline void dod(long long &a, long long b)
{
	a+=b;
	if (a>=mod)
		a-=mod;
}

inline void mini(int &a, int b)
{
	a=min(a, b);
}

int main()
{
	scanf("%d", &n);
	while(1)
	{
		char x=getchar();
		if (x==EOF)
			break;
		if (x>='0' && x<='9')
		{
			num*=10;
			num+=x-'0';
			continue;
		}
		if (x=='~')
		{
			r=1;
			continue;
		}
		if (x=='^')
		{
			m++;
			continue;
		}
		if (num)
		{
			wek[m].push_back(make_pair(num, r));
			num=r=0;
		}
	}
	for (int i=1; i<=m; i++)
	{
		sort(wek[i].begin(), wek[i].end());
		wek2[i]=wek[i];
		dos[i]=i;
	}
	sort(dos+1, dos+1+m, [](int a, int b)->bool{return wek[a][0]<wek[b][0];});
	for (int i=1; i<=m; i++)
		wek[i]=wek2[dos[i]];
	
	for (int i=1; i<=m; i++)
	{
		wsk[i]=i+1;
		for (int j=0; j<wek[i].size(); j++)
			co[wek[i][j].first].push_back(i);
		co[wek[i].back().first+1].push_back(i);
	}
	po[0]=1;
	for (int i=1; i<=n; i++)
	{
		po[i]=po[i-1];
		for (int j=0; j<co[i].size(); j++)
			po[i]=max(po[i], co[i][j]+1);
		co[i].push_back(po[i]);
		reverse(co[i].begin(), co[i].end());
	}
	
	/*for (int i=1; i<=m; i++)
	{
		for (int j=0; j<wek[i].size(); j++)
			printf("%d(%d) ", wek[i][j].first, wek[i][j].second);
		printf("\n");
	}
	printf("\n");
	for (int i=1; i<=n; i++)
	{
		printf("na %d: ", i);
		for (int j=0; j<co[i].size(); j++)
			printf(" %d", co[i][j]);
		printf("\n");
	}
	printf("\n");
	for (int i=1; i<=n; i++)
		printf("%d ", po[i]);
	printf("\n\n");*/
	dp[1]=1;
	for (int i=1; i<=n; i++)
	{
		nast[po[i]][0]=nast[po[i]][1]=po[i];
		for (int j=0; j<co[i].size(); j++)
		{
			int x=co[i][j];
			if (j)
			{
				//printf("robie %d w %d\n", x, i);
				nast[x][0]=nast[wsk[x]][0];
				nast[x][1]=nast[wsk[x]][1];
				long long w=dp[x];
				dp[x]=0;
				if (tab(x, i)>=0)
				{
					mini(nast[x][tab(x, i)], x);
					dod(dp[nast[x][tab(x, i)]], w);
					dod(dp[nast[x][tab(x, i)^1]], w);
					wsk[x]=nast[wsk[x]][tab(x, i)];
				}
				else
				{
					nast[x][0]=nast[x][1]=0;
				}
			}
			else
			{
				dod(dp[x], dp[x]);
			}
		}
		/*for (int j=1; j<=m+1; j++)
			printf("%lld ", dp[j]);
		printf("\n");
		for (int j=1; j<=m; j++)
			printf("%d ", wsk[j]);
		printf("\n\n");*/
	}
	printf("%lld\n", dp[m+1]);
	return 0;
}