#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

using poly=vector<int>;

void remove_padding_zeros(poly& p)
{
  size_t i = p.size();
  for(; i > 0; i--) if (p[i - 1] != 0) break;
  p.resize(i);
 
  // [1,2,0,0,0]  -in
  // [1,2]  -out
}

poly operator+(const poly &a, const poly &b) {
  poly result;
  size_t size = max(a.size(), b.size());
  result.resize(size);
  for (size_t i=0;i<a.size();i++)
    result[i] = a[i];
  for (size_t i=0;i<b.size();i++)
    result[i] += b[i];
  remove_padding_zeros(result);
  return result;
  
}

poly operator-(const poly &a, const poly &b) {
  poly result;
  size_t size = max(a.size(), b.size());
  result.resize(size);
  for (size_t i=0;i<a.size();i++)
    result[i] = a[i];
  for (size_t i=0;i<b.size();i++)
    result[i] -= b[i];
  remove_padding_zeros(result);
  return result; 
}

poly operator*(const poly &a, const poly &b) {
  poly result;
  size_t size = a.size() + b.size();
  result.resize(size);
  for (size_t i=0;i<a.size();i++) {
    for (size_t j=0;j<b.size();j++) {
      result[i+j] += a[i] * b[j];
    }
  }
  remove_padding_zeros(result);
  return result;
}

poly operator%(const poly &a, const poly &b) {
  poly result;

  //horner's polynomial evaluation method
  for (size_t i=a.size(); i>0; i--) {
    result = result * b;
    result = result + poly(1, a[i-1]);
  }
  remove_padding_zeros(result);
  return result;
}

struct Token {
  bool number;
  int value;
  char op;
  Token(){}
  Token(int value) { 
    number = true;
    this->value = value; 
  }
  Token(char c) { 
    number = false;
    this->op = c; 
  }
};

//istream& getline(istream&a, std::string&out);

bool isSingleTokenChar(char c ) {
  switch(c) {
    case '=':
    case '+':
    case '-':
    case '*':
    case 'd':
    case 'p':
    case 'e':
    case 'c':
    case 'q':
    case 'x':
      return true;
    default:
      return false;
  }
}

std::istream& readToken(std::istream&a, Token&out){
  std::string s;
  if(a >> s){
    if (s.length()>0 && isSingleTokenChar(s[0])) {
      out = Token(s[0]);
    }
    std::istringstream ss(s);
    int i;
    if (ss >> i){
      out = Token(i);
    }
  }
  return a;
}


int main() {
  std::stack<poly> s;
  Token t;

  while (readToken(std::cin, t)){
    if (t.number) {   //123   === 123*x^0 ==== [123]
      s.push(poly(1, t.value));
      continue;
    }

#define CHECKSTACK(SIZE) \
    if(s.size() < SIZE){ \
        std::cerr << "Wrong use of the stack!" << std::endl; \
        continue; \
      }


    if(t.op == 'q'){
      break;
    }

    if(t.op == 'p'){
      CHECKSTACK(1)
      // s.top() == [5,4,3]   --- 5*x^0 + 4*x^1 + 3*x^2
      poly& p = s.top();
      size_t s = 0;
      for(const auto& el : p) {
        std::cout << el << "*x^" << s;
        s++;
        if(s < p.size()) std::cout << " + ";
      }

      std::cout << std::endl;
      
      //std::cout << s.top() << std::endl;
      continue;
    }

    if (t.op == 'c') {
      CHECKSTACK(1)
      s.push(s.top());
      continue;
    }

    if (t.op == 'e') {
      CHECKSTACK(2)
      poly a = s.top();
      s.pop();
      poly b = s.top();
      s.pop();
      s.push(a);
      s.push(b);
      continue;
    } 

    if (t.op == 'd') {
      CHECKSTACK(1)
      s.pop();
      continue;
    }

    if (t.op == 'x') {
      poly p;
      p.resize(2);
      p[0] = 0; p[1] = 1;
      s.push(p);
      continue;
    }

    
#define OP(ch, o) \
    if(t.op==ch){ \
      if(s.size()<2){ \
        std::cerr << "There is less than 2 elements" << std::endl; \
        continue;\
      }\
      auto a = s.top();\
      s.pop();\
      auto b = s.top();\
      s.pop();\
      s.push(b o a);\
      continue;\
    }

    OP('+', +)
    OP('-', -)
    OP('*', *)
    OP('=', %)
#undef OP

  }

  return 0;
}


