#include <bits/stdc++.h>
using namespace std;
long long mod;
template <class T>
class treap {
  struct tree {
    T key;
    int cnt;
    long long mul;
    int pr;
    int size;
    tree *l, *r;
    tree(T _key = T(), int _pr = 0, tree* _l = NULL, tree* _r = NULL)
        : key(_key) {
      pr = _pr;
      l = _l;
      r = _r;
      size = 1;
      cnt = 1;
      mul = 1;
    }
  };
  tree* root;
  void update(tree*& t) {
    if (!t) return;
    t->size = (t->l ? t->l->size : 0) + (t->r ? t->r->size : 0) + 1;
  }
  void push(tree*& t) {
    if (!t) return;
    if (t->l) t->l->mul = (t->l->mul * t->mul) % mod;
    if (t->r) t->r->mul = (t->r->mul * t->mul) % mod;
    t->key = (t->key * t->mul) % mod;
    t->mul = 1;
  }
  void merge(tree*& t, tree* l, tree* r) {
    push(l);
    push(r);
    if (!l)
      t = r;
    else if (!r)
      t = l;
    else if (l->pr > r->pr) {
      merge(l->r, l->r, r);
      t = l;
    } else {
      merge(r->l, l, r->l);
      t = r;
    }
    update(t);
  }
  void split(tree* t, T& skey, tree*& l, tree*& r) {
    push(t);
    if (!t)
      l = r = NULL;
    else if (t->key < skey) {
      split(t->r, skey, t->r, r);
      l = t;
    } else {
      split(t->l, skey, l, t->l);
      r = t;
    }
    update(l);
    update(r);
  }
  void insert(tree*& t, tree* x) {
    push(t);
    if (!t)
      t = x;
    else if (t->pr < x->pr) {
      split(t, x->key, x->l, x->r);
      t = x;
    } else if (x->key < t->key)
      insert(t->l, x);
    else if (t->key < x->key)
      insert(t->r, x);
    update(t);
  }
  void erase(tree*& t, T& key) {
    if (!t) return;
    if (key < t->key)
      erase(t->l, key);
    else if (t->key < key)
      erase(t->r, key);
    else
      merge(t, t->l, t->r);
    update(t);
  }

 public:
  treap() {
    root = NULL;
    srand(time(0) + clock());
  }
  int size() { return (root ? root->size : 0); }
  void insert(T key) {
    tree* t = root;
    while (t) {
      push(t);
      if (key < t->key)
        t = t->l;
      else if (t->key < key)
        t = t->r;
      else {
        ++t->cnt;
        return;
      }
    }
    insert(root, new tree(key, rand() << 15 | rand()));
  }
  void erase(T key) { erase(root, key); }
  T nth(int n) {
    tree* t = root;
    while (t) {
      if (n == (t->l ? t->l->size : 0) + 1) return t->key;
      if (n <= (t->l ? t->l->size : 0))
        t = t->l;
      else {
        n -= (t->l ? t->l->size : 0) + 1;
        t = t->r;
      }
    }
  }
  int countof(T key) {
    tree* t = root;
    while (t) {
      push(t);
      if (key < t->key)
        t = t->l;
      else if (t->key < key)
        t = t->r;
      else
        return t->cnt;
    }
    return 0;
  }
  void multiply(T val) {
    if (!root) return;
    root->mul = (root->mul * val) % mod;
    push(root);
  }
  void print(tree* t) {
    if (!t) return;
    push(t);
    print(t->l);
    cout << "(" << t->key << ' ' << t->cnt << ") ";
    print(t->r);
  }
  void print() {
    print(root);
    cout << endl;
  }
};
int n, m;
int i, j, k, l;
long long p[111111];
long long q[111111];
long long K, B;
treap<long long> t;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> K >> B >> n;
  for (int _ = 0; _ < n; ++_) cin >> (p)[_];
  ;
  mod = K - 1;
  long long res = 0;
  long long zcnt = 0;
  if (B % mod == 0) {
    int l, r;
    for (i = 0; i < n; ++i) q[i] = p[i];
    for (i = 0; i < n; ++i)
      if (!q[i]) {
        for (j = i; j < n && !q[j]; ++j) q[j] = -1;
        long long d = j - i;
        res += (d + 1) * d / 2;
      }
    if (B == 0) {
      cout << res << endl;
      return 0;
    } else {
      zcnt = res;
      res = 0;
    }
  }
  B %= mod;
  long long x = 0;
  t.insert(x);
  for (i = 0; i < n; ++i) {
    x = x * K + p[i];
    x %= mod;
    t.multiply(K);
    res += t.countof(((x - B) % mod + mod) % mod);
    t.insert(x);
  }
  cout << res - zcnt << endl;
  return 0;
}

### Prompt

In cpp, your task is to solve the following problem:

You know that the Martians use a number system with base k. Digit b (0 ≤ b < k) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year b (by Martian chronology).

A digital root d(x) of number x is a number that consists of a single digit, resulting after cascading summing of all digits of number x. Word "cascading" means that if the first summing gives us a number that consists of several digits, then we sum up all digits again, and again, until we get a one digit number.

For example, d(35047) = d((3 + 5 + 0 + 4)7) = d(157) = d((1 + 5)7) = d(67) = 67. In this sample the calculations are performed in the 7-base notation.

If a number's digital root equals b, the Martians also call this number lucky.

You have string s, which consists of n digits in the k-base notation system. Your task is to find, how many distinct substrings of the given string are lucky numbers. Leading zeroes are permitted in the numbers.

Note that substring s[i... j] of the string s = a1a2... an (1 ≤ i ≤ j ≤ n) is the string aiai + 1... aj. Two substrings s[i1... j1] and s[i2... j2] of the string s are different if either i1 ≠ i2 or j1 ≠ j2.

Input

The first line contains three integers k, b and n (2 ≤ k ≤ 109, 0 ≤ b < k, 1 ≤ n ≤ 105).

The second line contains string s as a sequence of n integers, representing digits in the k-base notation: the i-th integer equals ai (0 ≤ ai < k) — the i-th digit of string s. The numbers in the lines are space-separated.

Output

Print a single integer — the number of substrings that are lucky numbers.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

10 5 6
3 2 0 5 6 1


Output

5

Input

7 6 4
3 5 0 4


Output

1

Input

257 0 3
0 0 256


Output

3

Note

In the first sample the following substrings have the sought digital root: s[1... 2] = "3 2", s[1... 3] = "3 2 0", s[3... 4] = "0 5", s[4... 4] = "5" and s[2... 6] = "2 0 5 6 1".

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod;
template <class T>
class treap {
  struct tree {
    T key;
    int cnt;
    long long mul;
    int pr;
    int size;
    tree *l, *r;
    tree(T _key = T(), int _pr = 0, tree* _l = NULL, tree* _r = NULL)
        : key(_key) {
      pr = _pr;
      l = _l;
      r = _r;
      size = 1;
      cnt = 1;
      mul = 1;
    }
  };
  tree* root;
  void update(tree*& t) {
    if (!t) return;
    t->size = (t->l ? t->l->size : 0) + (t->r ? t->r->size : 0) + 1;
  }
  void push(tree*& t) {
    if (!t) return;
    if (t->l) t->l->mul = (t->l->mul * t->mul) % mod;
    if (t->r) t->r->mul = (t->r->mul * t->mul) % mod;
    t->key = (t->key * t->mul) % mod;
    t->mul = 1;
  }
  void merge(tree*& t, tree* l, tree* r) {
    push(l);
    push(r);
    if (!l)
      t = r;
    else if (!r)
      t = l;
    else if (l->pr > r->pr) {
      merge(l->r, l->r, r);
      t = l;
    } else {
      merge(r->l, l, r->l);
      t = r;
    }
    update(t);
  }
  void split(tree* t, T& skey, tree*& l, tree*& r) {
    push(t);
    if (!t)
      l = r = NULL;
    else if (t->key < skey) {
      split(t->r, skey, t->r, r);
      l = t;
    } else {
      split(t->l, skey, l, t->l);
      r = t;
    }
    update(l);
    update(r);
  }
  void insert(tree*& t, tree* x) {
    push(t);
    if (!t)
      t = x;
    else if (t->pr < x->pr) {
      split(t, x->key, x->l, x->r);
      t = x;
    } else if (x->key < t->key)
      insert(t->l, x);
    else if (t->key < x->key)
      insert(t->r, x);
    update(t);
  }
  void erase(tree*& t, T& key) {
    if (!t) return;
    if (key < t->key)
      erase(t->l, key);
    else if (t->key < key)
      erase(t->r, key);
    else
      merge(t, t->l, t->r);
    update(t);
  }

 public:
  treap() {
    root = NULL;
    srand(time(0) + clock());
  }
  int size() { return (root ? root->size : 0); }
  void insert(T key) {
    tree* t = root;
    while (t) {
      push(t);
      if (key < t->key)
        t = t->l;
      else if (t->key < key)
        t = t->r;
      else {
        ++t->cnt;
        return;
      }
    }
    insert(root, new tree(key, rand() << 15 | rand()));
  }
  void erase(T key) { erase(root, key); }
  T nth(int n) {
    tree* t = root;
    while (t) {
      if (n == (t->l ? t->l->size : 0) + 1) return t->key;
      if (n <= (t->l ? t->l->size : 0))
        t = t->l;
      else {
        n -= (t->l ? t->l->size : 0) + 1;
        t = t->r;
      }
    }
  }
  int countof(T key) {
    tree* t = root;
    while (t) {
      push(t);
      if (key < t->key)
        t = t->l;
      else if (t->key < key)
        t = t->r;
      else
        return t->cnt;
    }
    return 0;
  }
  void multiply(T val) {
    if (!root) return;
    root->mul = (root->mul * val) % mod;
    push(root);
  }
  void print(tree* t) {
    if (!t) return;
    push(t);
    print(t->l);
    cout << "(" << t->key << ' ' << t->cnt << ") ";
    print(t->r);
  }
  void print() {
    print(root);
    cout << endl;
  }
};
int n, m;
int i, j, k, l;
long long p[111111];
long long q[111111];
long long K, B;
treap<long long> t;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> K >> B >> n;
  for (int _ = 0; _ < n; ++_) cin >> (p)[_];
  ;
  mod = K - 1;
  long long res = 0;
  long long zcnt = 0;
  if (B % mod == 0) {
    int l, r;
    for (i = 0; i < n; ++i) q[i] = p[i];
    for (i = 0; i < n; ++i)
      if (!q[i]) {
        for (j = i; j < n && !q[j]; ++j) q[j] = -1;
        long long d = j - i;
        res += (d + 1) * d / 2;
      }
    if (B == 0) {
      cout << res << endl;
      return 0;
    } else {
      zcnt = res;
      res = 0;
    }
  }
  B %= mod;
  long long x = 0;
  t.insert(x);
  for (i = 0; i < n; ++i) {
    x = x * K + p[i];
    x %= mod;
    t.multiply(K);
    res += t.countof(((x - B) % mod + mod) % mod);
    t.insert(x);
  }
  cout << res - zcnt << endl;
  return 0;
}

```