#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int mod=313;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
int n,a[N],f[N];
struct Complex{
db r,i;
Complex() {}
Complex(db r,db i):r(r),i(i) {}
Complex operator + (const Complex &t) const {
return Complex(r+t.r,i+t.i);
}
Complex operator - (const Complex &t) const {
return Complex(r-t.r,i-t.i);
}
Complex operator * (const Complex &t) const {
return Complex(r*t.r-i*t.i,r*t.i+i*t.r);
}
}A[2*N],B[2*N];
void FFT(Complex x[],int n,int rev) {
int i,j,k,t,ds;
Complex w,u,wn;
for (i=1;i<n;i++) {
for (j=0,t=i,k=n>>1;k;k>>=1,t>>=1) j=j<<1|t&1;
if (i<j) swap(x[i],x[j]);
}
for (i=2,ds=1;i<=n;ds=i,i<<=1) {
w=Complex(1,0);wn=Complex(cos(rev*2*pi/i),sin(rev*2*pi/i));
for (j=0;j<ds;j++,w=w*wn)
for (k=j;k<n;k+=i) {
u=w*x[k+ds];x[k+ds]=x[k]-u;x[k]=x[k]+u;
}
}
if (rev==-1) for (i=0;i<n;i++) x[i].r/=n;
}
void cdq(int l,int r) {
if (l==r) { (f[l]+=a[l])%=mod;return ; }
int i,mid=(l+r)>>1,len=1,len1=mid-l+1,len2=r-l;
cdq(l,mid);
while (len<len2) len<<=1;
for (i=0;i<len1;i++) A[i]=Complex(f[l+i],0);
for (i=len1;i<len;i++) A[i]=Complex(0,0);
for (i=0;i<len2;i++) B[i]=Complex(a[i+1],0);
for (i=len2;i<len;i++) B[i]=Complex(0,0);
FFT(A,len,1);FFT(B,len,1);
for (i=0;i<len;i++) A[i]=A[i]*B[i];
FFT(A,len,-1);
for (i=mid+1;i<=r;i++) (f[i]+=(int)(A[i-l-1].r+0.5))%=mod;
cdq(mid+1,r);
}
int main()
{
while (scanf("%d", &n)&&n) {
memset(f,0,sizeof(f));
for (int i=1;i<=n;i++) scanf("%d", &a[i]),a[i]%=mod;
cdq(1,n);
printf("%d\n", (f[n]+mod)%mod);
}
return 0;
}