#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline bool isSquare(T x) {
  T y = sqrt(x + 0.5);
  return (y * y) == x;
}
template <class T1, class T2>
inline T1 gcd(T1 a, T2 b) {
  return b ? gcd(b, a % b) : a;
}
template <class T1, class T2>
inline T1 eqMin(T1 &x, const T2 &y) {
  if (T1(y) < x) return x = y;
  return x;
}
template <class T1, class T2>
inline T1 eqMax(T1 &x, const T2 &y) {
  if (T1(y) > x) return x = y;
  return x;
}
template <class T1, class T2>
inline T1 min(const T1 &x, const T2 &y) {
  return x < (T1)y ? x : (T1)y;
}
template <class T1, class T2>
inline T1 max(T1 &x, const T2 &y) {
  return x > (T1)y ? x : (T1)y;
}
template <typename T>
inline T getint() {
  T x = 0, p = 1;
  char ch;
  do {
    ch = getchar();
  } while (ch <= ' ');
  if (ch == '-') p = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * p;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T>
auto operator<<(ostream &os, const T &v)
    -> decltype(v.begin(), v.end(), declval<ostream &>()) {
  os << '{';
  bool was = false;
  for (const auto &x : v) {
    if (was) os << ", ";
    was = true;
    os << x;
  }
  os << '}';
  return os;
}
template <size_t sz>
ostream &operator<<(ostream &os, const bitset<sz> &bit) {
  os << '/';
  for (int i = 0; i < sz; i++) os << bit[i];
  os << '\\';
  return os;
}
const double PI = acos(-1);
const double EPS = 1e-8;
const int INF = (int)2e9;
const long long LINF = (long long)2e18;
const long long MOD = (long long)998244353;
const int MAXN = (int)1e6 + 10;
namespace NumNamespace {
int modNum = 998244353;
struct Num {
  int value;
  inline Num() : value(0) {}
  inline Num(const long long &x)
      : value((x + (long long)modNum * (modNum + 100)) % modNum) {}
  inline Num operator+(Num other) const { return value + other.value; }
  inline Num operator+=(Num other) { return (*this) = value + other.value; }
  inline Num operator-(Num other) const { return value - other.value + modNum; }
  inline Num operator-=(Num other) {
    return (*this) = value - other.value + modNum;
  }
  inline Num operator*(Num other) const {
    return (long long)value * other.value;
  }
  inline Num operator*=(Num other) {
    return (*this) = (long long)value * other.value;
  }
  inline Num power(long long step) const {
    Num res(1);
    Num x = (*this);
    while (step) {
      if (step & 1) {
        res *= x;
      }
      step >>= 1;
      x *= x;
    }
    return res;
  }
  inline Num operator/(Num other) const {
    return (*this) * other.power(modNum - 2);
  }
  inline Num operator/=(Num other) {
    return *this = (*this) * other.power(modNum - 2);
  }
  inline int conv() const { return value; }
  inline bool operator==(Num other) const { return value == other.value; }
  inline bool operator!=(Num other) const { return value != other.value; }
};
}  // namespace NumNamespace
using namespace NumNamespace;
int n;
Num fact[MAXN], c[MAXN], ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  fact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = fact[i - 1] * i;
  }
  for (int i = 0; i <= n; i++) {
    c[i] = fact[n] / fact[i] / fact[n - i];
  }
  for (int i = 1, k = 1; i <= n; i++, k *= -1) {
    auto val = Num(2 * k) * c[i] * Num(3).power(i) *
               Num(3).power((long long)n * (n - i));
    ans += val;
  };
  for (int i = 0, k = -1; i < n; i++, k *= -1) {
    Num step = Num(3).power(i) * Num(-1);
    auto val = Num(3 * k) * c[i] * ((Num(1) + step).power(n) - step.power(n));
    ans += val;
  }
  cout << ans.value;
}
